Safer thread synchronization for song timeline
This commit is contained in:
parent
093da1c9e7
commit
2a31946f67
@ -296,6 +296,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
|
||||
private int[] mSongs;
|
||||
private ArrayList<Song> mSongTimeline;
|
||||
private Object mSongTimelineLock = new Object();
|
||||
private int mCurrentSong = 0;
|
||||
private int mQueuePos = 0;
|
||||
private int mState = STATE_NORMAL;
|
||||
@ -527,7 +528,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
if (song == null)
|
||||
return;
|
||||
|
||||
synchronized (mSongTimelineLock) {
|
||||
mCurrentSong += delta;
|
||||
}
|
||||
|
||||
try {
|
||||
mMediaPlayer.reset();
|
||||
@ -545,11 +548,13 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
|
||||
getSong(+2);
|
||||
|
||||
synchronized (mSongTimelineLock) {
|
||||
while (mCurrentSong > 15) {
|
||||
mSongTimeline.remove(0);
|
||||
--mCurrentSong;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onCompletion(MediaPlayer player)
|
||||
{
|
||||
@ -572,8 +577,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
return new Song(mSongs[mRandom.nextInt(mSongs.length)]);
|
||||
}
|
||||
|
||||
private synchronized Song getSong(int delta)
|
||||
private Song getSong(int delta)
|
||||
{
|
||||
synchronized (mSongTimelineLock) {
|
||||
if (mSongTimeline == null)
|
||||
return null;
|
||||
|
||||
@ -594,6 +600,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
|
||||
return mSongTimeline.get(pos);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateWidgets()
|
||||
{
|
||||
@ -635,12 +642,14 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
||||
String text = getResources().getString(R.string.enqueued, song.title);
|
||||
Toast.makeText(ContextApplication.getContext(), text, Toast.LENGTH_SHORT).show();
|
||||
|
||||
synchronized (mSongTimelineLock) {
|
||||
int i = mCurrentSong + 1 + mQueuePos++;
|
||||
if (i < mSongTimeline.size())
|
||||
mSongTimeline.set(i, song);
|
||||
else
|
||||
mSongTimeline.add(song);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case TRACK_CHANGED:
|
||||
setCurrentSong(+1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user