From df570d507c8ccfaa220bcc5b77a4697cd84ff2c4 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 28 Aug 2011 03:45:53 -0500 Subject: [PATCH] Use song duration data from MediaStore Allows the duration to be displayed before the song starts playing. Also add a missing synchronized around a call to wait() --- .../kreed/vanilla/FullPlaybackActivity.java | 38 +++++++++---------- src/org/kreed/vanilla/PlaybackService.java | 12 ------ src/org/kreed/vanilla/Song.java | 9 ++++- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/org/kreed/vanilla/FullPlaybackActivity.java b/src/org/kreed/vanilla/FullPlaybackActivity.java index 79b9717b..2890fac9 100644 --- a/src/org/kreed/vanilla/FullPlaybackActivity.java +++ b/src/org/kreed/vanilla/FullPlaybackActivity.java @@ -62,7 +62,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On private TextView mAlbum; private TextView mArtist; - private int mDuration; + private long mDuration; private boolean mSeekBarTracking; private boolean mPaused; @@ -204,10 +204,8 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On { super.onServiceReady(); - PlaybackService service = ContextApplication.getService(); - if (mTitle != null) - mUiHandler.sendMessage(mUiHandler.obtainMessage(MSG_UPDATE_SONG, service.getSong(0))); - mDuration = service.getDuration(); + Song song = ContextApplication.getService().getSong(0); + mUiHandler.sendMessage(mUiHandler.obtainMessage(MSG_UPDATE_SONG, song)); } @Override @@ -216,12 +214,8 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On super.receive(intent); if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) { - if (mTitle != null) { - Song song = intent.getParcelableExtra("song"); - mUiHandler.sendMessage(mUiHandler.obtainMessage(MSG_UPDATE_SONG, song)); - } - mDuration = ContextApplication.getService().getDuration(); - mUiHandler.sendEmptyMessage(MSG_UPDATE_PROGRESS); + Song song = intent.getParcelableExtra("song"); + mUiHandler.sendMessage(mUiHandler.obtainMessage(MSG_UPDATE_SONG, song)); } } @@ -267,9 +261,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On /** * Converts a duration in milliseconds to [HH:]MM:SS */ - private String stringForTime(int ms) + private String stringForTime(long ms) { - int seconds = ms / 1000; + int seconds = (int)(ms / 1000); int hours = seconds / 3600; seconds -= hours * 3600; @@ -287,19 +281,18 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On */ private void updateProgress() { - if (mPaused || mControlsTop.getVisibility() != View.VISIBLE || (mState & PlaybackService.FLAG_PLAYING) == 0) - return; - - int position = ContextApplication.getService().getPosition(); + int position = ContextApplication.hasService() ? ContextApplication.getService().getPosition() : 0; if (!mSeekBarTracking) mSeekBar.setProgress(mDuration == 0 ? 0 : (int)(1000 * position / mDuration)); mSeekText.setText(stringForTime((int)position) + " / " + stringForTime(mDuration)); - // Try to update right when the duration increases by one second - long next = 1000 - position % 1000; - mUiHandler.removeMessages(MSG_UPDATE_PROGRESS); - mUiHandler.sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, next); + if (!mPaused && mControlsTop.getVisibility() == View.VISIBLE && (mState & PlaybackService.FLAG_PLAYING) != 1) { + // Try to update right when the duration increases by one second + long next = 1000 - position % 1000; + mUiHandler.removeMessages(MSG_UPDATE_PROGRESS); + mUiHandler.sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, next); + } } /** @@ -387,11 +380,14 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On mTitle.setText(null); mAlbum.setText(null); mArtist.setText(null); + mDuration = 0; } else { mTitle.setText(song.title); mAlbum.setText(song.album); mArtist.setText(song.artist); + mDuration = song.duration; } + updateProgress(); break; } case MSG_SHOW_NO_MEDIA: diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 554e6461..a69d604a 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -887,18 +887,6 @@ public final class PlaybackService extends Service implements Handler.Callback, } } - /** - * Returns the duration of the current song in milliseconds. - */ - public int getDuration() - { - if (mMediaPlayer == null) - return 0; - synchronized (mMediaPlayer) { - return mMediaPlayer.getDuration(); - } - } - /** * Returns the position of the current song in the song timeline. * diff --git a/src/org/kreed/vanilla/Song.java b/src/org/kreed/vanilla/Song.java index 46f166e5..e7ea0a79 100644 --- a/src/org/kreed/vanilla/Song.java +++ b/src/org/kreed/vanilla/Song.java @@ -85,7 +85,8 @@ public class Song implements Parcelable { MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, - MediaStore.Audio.Media.ALBUM_ID + MediaStore.Audio.Media.ALBUM_ID, + MediaStore.Audio.Media.DURATION }; /** @@ -115,6 +116,11 @@ public class Song implements Parcelable { */ public String artist; + /** + * Length of the song in milliseconds. + */ + public long duration; + /** * Song flags. Currently FLAG_RANDOM or 0. */ @@ -284,6 +290,7 @@ public class Song implements Parcelable { album = cursor.getString(3); artist = cursor.getString(4); albumId = cursor.getLong(5); + duration = cursor.getLong(6); } /**