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;
|
||||||
|
|
||||||
mCurrentSong += delta;
|
synchronized (mSongTimelineLock) {
|
||||||
|
mCurrentSong += delta;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mMediaPlayer.reset();
|
mMediaPlayer.reset();
|
||||||
@ -545,9 +548,11 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
|
|||||||
|
|
||||||
getSong(+2);
|
getSong(+2);
|
||||||
|
|
||||||
while (mCurrentSong > 15) {
|
synchronized (mSongTimelineLock) {
|
||||||
mSongTimeline.remove(0);
|
while (mCurrentSong > 15) {
|
||||||
--mCurrentSong;
|
mSongTimeline.remove(0);
|
||||||
|
--mCurrentSong;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -572,27 +577,29 @@ 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)
|
||||||
{
|
{
|
||||||
if (mSongTimeline == null)
|
synchronized (mSongTimelineLock) {
|
||||||
return null;
|
if (mSongTimeline == null)
|
||||||
|
|
||||||
int pos = mCurrentSong + delta;
|
|
||||||
|
|
||||||
if (pos < 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
int size = mSongTimeline.size();
|
|
||||||
if (pos > size)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (pos == size) {
|
|
||||||
if (mSongs == null)
|
|
||||||
return null;
|
return null;
|
||||||
mSongTimeline.add(randomSong());
|
|
||||||
|
int pos = mCurrentSong + delta;
|
||||||
|
|
||||||
|
if (pos < 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
int size = mSongTimeline.size();
|
||||||
|
if (pos > size)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (pos == size) {
|
||||||
|
if (mSongs == null)
|
||||||
|
return null;
|
||||||
|
mSongTimeline.add(randomSong());
|
||||||
|
}
|
||||||
|
|
||||||
|
return mSongTimeline.get(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mSongTimeline.get(pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateWidgets()
|
private void updateWidgets()
|
||||||
@ -635,11 +642,13 @@ 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();
|
||||||
|
|
||||||
int i = mCurrentSong + 1 + mQueuePos++;
|
synchronized (mSongTimelineLock) {
|
||||||
if (i < mSongTimeline.size())
|
int i = mCurrentSong + 1 + mQueuePos++;
|
||||||
mSongTimeline.set(i, song);
|
if (i < mSongTimeline.size())
|
||||||
else
|
mSongTimeline.set(i, song);
|
||||||
mSongTimeline.add(song);
|
else
|
||||||
|
mSongTimeline.add(song);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TRACK_CHANGED:
|
case TRACK_CHANGED:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user