diff --git a/src/org/kreed/tumult/CoverView.java b/src/org/kreed/tumult/CoverView.java index e9fd8a25..0227b59f 100755 --- a/src/org/kreed/tumult/CoverView.java +++ b/src/org/kreed/tumult/CoverView.java @@ -47,7 +47,12 @@ public class CoverView extends View { { mListener = listener; } - + + public Song getActiveSong() + { + return mSongs[1]; + } + private RectF scale(Bitmap bitmap, int maxWidth, int maxHeight) { float bitmapWidth = bitmap.getWidth(); diff --git a/src/org/kreed/tumult/IMusicPlayerWatcher.aidl b/src/org/kreed/tumult/IMusicPlayerWatcher.aidl index 6cefdcfc..388a91d8 100644 --- a/src/org/kreed/tumult/IMusicPlayerWatcher.aidl +++ b/src/org/kreed/tumult/IMusicPlayerWatcher.aidl @@ -6,5 +6,4 @@ oneway interface IMusicPlayerWatcher { void previousSong(in Song playingSong, in Song nextForwardSong); void nextSong(in Song playingSong, in Song nextBackwardSong); void stateChanged(in int oldState, in int newState); - void mediaLengthChanged(in long startTime, in int duration); } \ No newline at end of file diff --git a/src/org/kreed/tumult/IPlaybackService.aidl b/src/org/kreed/tumult/IPlaybackService.aidl index a29f1687..fdc3365a 100644 --- a/src/org/kreed/tumult/IPlaybackService.aidl +++ b/src/org/kreed/tumult/IPlaybackService.aidl @@ -8,7 +8,7 @@ interface IPlaybackService { Song[] getCurrentSongs(); int getState(); - long getStartTime(); + int getPosition(); int getDuration(); void previousSong(); diff --git a/src/org/kreed/tumult/MusicPlayer.java b/src/org/kreed/tumult/MusicPlayer.java index e876b049..d165517e 100644 --- a/src/org/kreed/tumult/MusicPlayer.java +++ b/src/org/kreed/tumult/MusicPlayer.java @@ -42,14 +42,14 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, { return mState; } - - public long getStartTime() + + public int getPosition() { if (mMediaPlayer == null) return 0; - return MusicPlayer.this.getStartTime(); + return mMediaPlayer.getCurrentPosition(); } - + public int getDuration() { if (mMediaPlayer == null) @@ -91,7 +91,6 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, long position = (long)mMediaPlayer.getDuration() * progress / 1000; mMediaPlayer.seekTo((int)position); - mediaLengthChanged(); } }; @@ -261,14 +260,9 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, if (mSongs == null && mState == STATE_NORMAL) setState(STATE_NO_MEDIA); } - - private void play() + + private Notification createNotfication() { - if (mHeadsetOnly && !mPlugged) - return; - - mMediaPlayer.start(); - Song song = getSong(0); RemoteViews views = new RemoteViews(mService.getPackageName(), R.layout.statusbar); @@ -282,9 +276,17 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, notification.flags |= Notification.FLAG_ONGOING_EVENT; Intent intent = new Intent(mService, NowPlayingActivity.class); notification.contentIntent = PendingIntent.getActivity(mService, 0, intent, 0); - - mService.startForegroundCompat(NOTIFICATION_ID, notification); + return notification; + } + + private void play() + { + if (mHeadsetOnly && !mPlugged) + return; + + mMediaPlayer.start(); + mService.startForegroundCompat(NOTIFICATION_ID, createNotfication()); setState(STATE_PLAYING); } @@ -311,6 +313,16 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, mCurrentSong += delta; + try { + mMediaPlayer.reset(); + mMediaPlayer.setDataSource(song.path); + mMediaPlayer.prepare(); + if (mState == STATE_PLAYING) + play(); + } catch (IOException e) { + Log.e("Tumult", "IOException", e); + } + Song newSong = getSong(delta); int i = mWatchers.beginBroadcast(); while (--i != -1) { @@ -325,18 +337,6 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, } mWatchers.finishBroadcast(); - try { - mMediaPlayer.reset(); - mMediaPlayer.setDataSource(song.path); - mMediaPlayer.prepare(); - if (mState == STATE_PLAYING) - play(); - } catch (IOException e) { - Log.e("Tumult", "IOException", e); - } - - mediaLengthChanged(); - getSong(+2); while (mCurrentSong > 15) { @@ -344,28 +344,6 @@ public class MusicPlayer implements Runnable, MediaPlayer.OnCompletionListener, --mCurrentSong; } } - - private long getStartTime() - { - int position = mMediaPlayer.getCurrentPosition(); - return System.currentTimeMillis() - position; - } - - private void mediaLengthChanged() - { - long start = getStartTime(); - int duration = mMediaPlayer.getDuration(); - - int i = mWatchers.beginBroadcast(); - while (--i != -1) { - try { - mWatchers.getBroadcastItem(i).mediaLengthChanged(start, duration); - } catch (RemoteException e) { - // Null elements will be removed automatically - } - } - mWatchers.finishBroadcast(); - } public void onCompletion(MediaPlayer player) { diff --git a/src/org/kreed/tumult/NowPlayingActivity.java b/src/org/kreed/tumult/NowPlayingActivity.java index 763c9546..8e834a19 100644 --- a/src/org/kreed/tumult/NowPlayingActivity.java +++ b/src/org/kreed/tumult/NowPlayingActivity.java @@ -40,7 +40,6 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se private TextView mSeekText; private int mState; - private long mStartTime; private int mDuration; private boolean mSeekBarTracking; @@ -157,33 +156,38 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se reconnect(); } + private boolean requestSongInfo(Song playingSong) + { + try { + mDuration = mService.getDuration(); + mHandler.sendEmptyMessage(UPDATE_PROGRESS); + } catch (RemoteException e) { + } + + if (!playingSong.equals(mCoverView.getActiveSong())) { + runOnUiThread(new Runnable() { + public void run() + { + refreshSongs(); + } + }); + return false; + } + + return true; + } + private IMusicPlayerWatcher mWatcher = new IMusicPlayerWatcher.Stub() { public void nextSong(final Song playingSong, final Song forwardSong) { - if (mCoverView.mSongs[1] != null && mCoverView.mSongs[1].path.equals(playingSong.path)) { + if (requestSongInfo(playingSong)) mCoverView.setForwardSong(forwardSong); - } else { - runOnUiThread(new Runnable() { - public void run() - { - refreshSongs(); - } - }); - } } public void previousSong(final Song playingSong, final Song backwardSong) { - if (mCoverView.mSongs[1] != null && mCoverView.mSongs[1].path.equals(playingSong.path)) { + if (requestSongInfo(playingSong)) mCoverView.setBackwardSong(backwardSong); - } else { - runOnUiThread(new Runnable() { - public void run() - { - refreshSongs(); - } - }); - } } public void stateChanged(final int oldState, final int newState) @@ -195,22 +199,6 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se } }); } - - public void mediaLengthChanged(long startTime, int duration) - { - mStartTime = startTime; - mDuration = duration; - runOnUiThread(new Runnable() { - public void run() - { - if (mState != MusicPlayer.STATE_PLAYING) { - String text = mSeekText.getText().toString(); - text = text.substring(0, text.indexOf('/') + 2) + stringForTime(mDuration); - mSeekText.setText(text); - } - } - }); - } }; public void next() @@ -293,15 +281,6 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se mPlayPauseButton.requestFocus(); - if (mStartTime == 0) { - try { - mStartTime = mService.getStartTime(); - mDuration = mService.getDuration(); - } catch (RemoteException e) { - return; - } - } - updateProgress(); sendHideMessage(); } @@ -324,23 +303,29 @@ public class NowPlayingActivity extends Activity implements CoverViewWatcher, Se private void updateProgress() { - if (mState != MusicPlayer.STATE_PLAYING || mControlsTop.getVisibility() != View.VISIBLE) + if (mControlsTop.getVisibility() != View.VISIBLE) return; - long position = System.currentTimeMillis() - mStartTime; + int position; + try { + position = mService.getPosition(); + } catch (RemoteException e) { + return; + } + if (!mSeekBarTracking) - mSeekBar.setProgress((int)(1000 * position / mDuration)); + mSeekBar.setProgress(mDuration == 0 ? 0 : (int)(1000 * position / mDuration)); mSeekText.setText(stringForTime((int)position) + " / " + stringForTime(mDuration)); long next = 1000 - position % 1000; - mHandler.sendMessageDelayed(mHandler.obtainMessage(UPDATE_PROGRESS), next); + mHandler.removeMessages(UPDATE_PROGRESS); + mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, next); } private void sendHideMessage() { - Message message = mHandler.obtainMessage(HIDE); mHandler.removeMessages(HIDE); - mHandler.sendMessageDelayed(message, 3000); + mHandler.sendEmptyMessageDelayed(HIDE, 3000); } public void onClick(View view) diff --git a/src/org/kreed/tumult/Song.java b/src/org/kreed/tumult/Song.java index 8e451417..7f661810 100644 --- a/src/org/kreed/tumult/Song.java +++ b/src/org/kreed/tumult/Song.java @@ -76,6 +76,13 @@ public class Song implements Parcelable { return songs; } + public boolean equals(Song other) + { + if (other == null) + return false; + return path.equals(other.path); + } + public static Parcelable.Creator CREATOR = new Parcelable.Creator() { public Song createFromParcel(Parcel in) {