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