Make the position code work

Now we just request position from the service each time we update it.
This is somewhat more reliable and simple. The optimization the old
method provided was not worthwhile.
This commit is contained in:
Christopher Eby 2010-02-19 20:36:22 -06:00
parent b56fc0eeb0
commit 429aa68350
6 changed files with 75 additions and 101 deletions

View File

@ -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();

View File

@ -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);
}

View File

@ -8,7 +8,7 @@ interface IPlaybackService {
Song[] getCurrentSongs();
int getState();
long getStartTime();
int getPosition();
int getDuration();
void previousSong();

View File

@ -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)
{

View File

@ -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)

View File

@ -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<Song> CREATOR = new Parcelable.Creator<Song>() {
public Song createFromParcel(Parcel in)
{