Don't synchronize MediaPlayer

Preparing sometimes takes a long time and this causes a freeze in the playback
actively when it requests the song position.
This commit is contained in:
Christopher Eby 2011-09-18 09:35:34 -05:00
parent 5c45c4540a
commit e19ac9bca5

View File

@ -330,11 +330,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
} else if ("volume".equals(key)) { } else if ("volume".equals(key)) {
float volume = settings.getFloat("volume", 1.0f); float volume = settings.getFloat("volume", 1.0f);
mCurrentVolume = mUserVolume = volume; mCurrentVolume = mUserVolume = volume;
if (mMediaPlayer != null) { if (mMediaPlayer != null)
synchronized (mMediaPlayer) { mMediaPlayer.setVolume(volume, volume);
mMediaPlayer.setVolume(volume, volume);
}
}
} else if ("media_button".equals(key)) { } else if ("media_button".equals(key)) {
MediaButtonHandler.reloadPreference(); MediaButtonHandler.reloadPreference();
} else if ("use_idle_timeout".equals(key) || "idle_timeout".equals(key)) { } else if ("use_idle_timeout".equals(key) || "idle_timeout".equals(key)) {
@ -411,22 +408,18 @@ public final class PlaybackService extends Service implements Handler.Callback,
if ((toggled & FLAG_PLAYING) != 0) { if ((toggled & FLAG_PLAYING) != 0) {
if ((state & FLAG_PLAYING) != 0) { if ((state & FLAG_PLAYING) != 0) {
if (mMediaPlayerInitialized) { if (mMediaPlayerInitialized)
synchronized (mMediaPlayer) { mMediaPlayer.start();
mMediaPlayer.start();
}
}
if (mNotificationMode != NEVER) if (mNotificationMode != NEVER)
startForeground(NOTIFICATION_ID, new SongNotification(mCurrentSong, true)); startForeground(NOTIFICATION_ID, new SongNotification(mCurrentSong, true));
if (mWakeLock != null) if (mWakeLock != null)
mWakeLock.acquire(); mWakeLock.acquire();
} else { } else {
if (mMediaPlayerInitialized) { if (mMediaPlayerInitialized)
synchronized (mMediaPlayer) { mMediaPlayer.pause();
mMediaPlayer.pause();
}
}
if (mNotificationMode == ALWAYS) { if (mNotificationMode == ALWAYS) {
stopForeground(false); stopForeground(false);
mNotificationManager.notify(NOTIFICATION_ID, new SongNotification(mCurrentSong, false)); mNotificationManager.notify(NOTIFICATION_ID, new SongNotification(mCurrentSong, false));
@ -579,10 +572,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
if (mMediaPlayer == null) if (mMediaPlayer == null)
return null; return null;
synchronized (mMediaPlayer) { if (mMediaPlayer.isPlaying())
if (mMediaPlayer.isPlaying()) mMediaPlayer.stop();
mMediaPlayer.stop();
}
Song song = mTimeline.shiftCurrentSong(delta); Song song = mTimeline.shiftCurrentSong(delta);
mCurrentSong = song; mCurrentSong = song;
@ -624,13 +615,11 @@ public final class PlaybackService extends Service implements Handler.Callback,
private void processSong(Song song) private void processSong(Song song)
{ {
try { try {
synchronized (mMediaPlayer) { mMediaPlayerInitialized = false;
mMediaPlayer.reset(); mMediaPlayer.reset();
mMediaPlayer.setDataSource(song.path); mMediaPlayer.setDataSource(song.path);
mMediaPlayer.prepare(); mMediaPlayer.prepare();
if (!mMediaPlayerInitialized) mMediaPlayerInitialized = true;
mMediaPlayerInitialized = true;
}
if (mPendingSeek != 0) { if (mPendingSeek != 0) {
mMediaPlayer.seekTo(mPendingSeek); mMediaPlayer.seekTo(mPendingSeek);
@ -843,11 +832,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
mCurrentVolume = Math.max((float)(Math.pow(progress / 100f, 4) * mUserVolume), .01f); mCurrentVolume = Math.max((float)(Math.pow(progress / 100f, 4) * mUserVolume), .01f);
mHandler.sendMessageDelayed(mHandler.obtainMessage(FADE_OUT, progress, 0), 50); mHandler.sendMessageDelayed(mHandler.obtainMessage(FADE_OUT, progress, 0), 50);
} }
if (mMediaPlayer != null) { if (mMediaPlayer != null)
synchronized (mMediaPlayer) { mMediaPlayer.setVolume(mCurrentVolume, mCurrentVolume);
mMediaPlayer.setVolume(mCurrentVolume, mCurrentVolume);
}
}
break; break;
case PROCESS_STATE: case PROCESS_STATE:
processNewState(message.arg1, message.arg2); processNewState(message.arg1, message.arg2);
@ -881,11 +867,9 @@ public final class PlaybackService extends Service implements Handler.Callback,
*/ */
public int getPosition() public int getPosition()
{ {
if (mMediaPlayer == null) if (!mMediaPlayerInitialized)
return 0; return 0;
synchronized (mMediaPlayer) { return mMediaPlayer.getCurrentPosition();
return mMediaPlayer.getCurrentPosition();
}
} }
/** /**
@ -895,12 +879,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
*/ */
public void seekToProgress(int progress) public void seekToProgress(int progress)
{ {
if (mMediaPlayer == null) if (!mMediaPlayerInitialized)
return; return;
synchronized (mMediaPlayer) { long position = (long)mMediaPlayer.getDuration() * progress / 1000;
long position = (long)mMediaPlayer.getDuration() * progress / 1000; mMediaPlayer.seekTo((int)position);
mMediaPlayer.seekTo((int)position);
}
} }
@Override @Override