Make shure thread quits before service
This commit is contained in:
parent
f22b5157f5
commit
ac0dff7aa1
|
@ -161,18 +161,20 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private PlaybackState getPlaybackState() {
|
private synchronized PlaybackState getPlaybackState() {
|
||||||
try {
|
try {
|
||||||
return new PlaybackState(mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), isPlaying());
|
return new PlaybackState(mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), isPlaying());
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
// This isn't that nice way to handle this.
|
// This isn't that nice way to handle this.
|
||||||
// maybe there is a better way
|
// maybe there is a better way
|
||||||
|
Log.w(TAG, this + ": Got illegal state exception while creating playback state", e);
|
||||||
return PlaybackState.UNPREPARED;
|
return PlaybackState.UNPREPARED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void broadcastState() {
|
private void broadcastState() {
|
||||||
PlaybackState state = getPlaybackState();
|
PlaybackState state = getPlaybackState();
|
||||||
|
if(state == null) return;
|
||||||
Intent intent = new Intent(ACTION_PLAYBACK_STATE);
|
Intent intent = new Intent(ACTION_PLAYBACK_STATE);
|
||||||
intent.putExtra(EXTRA_PLAYBACK_STATE, state);
|
intent.putExtra(EXTRA_PLAYBACK_STATE, state);
|
||||||
sendBroadcast(intent);
|
sendBroadcast(intent);
|
||||||
|
@ -284,9 +286,6 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
|
||||||
break;
|
break;
|
||||||
case ACTION_PLAYBACK_STATE: {
|
case ACTION_PLAYBACK_STATE: {
|
||||||
PlaybackState playbackState = intent.getParcelableExtra(EXTRA_PLAYBACK_STATE);
|
PlaybackState playbackState = intent.getParcelableExtra(EXTRA_PLAYBACK_STATE);
|
||||||
Log.d(TAG, "playback state recieved: " + playbackState);
|
|
||||||
Log.d(TAG, "is unprepared: " + playbackState.equals(PlaybackState.UNPREPARED));
|
|
||||||
Log.d(TAG, "playing: " + playbackState.getPlayedTime());
|
|
||||||
if(!playbackState.equals(PlaybackState.UNPREPARED)) {
|
if(!playbackState.equals(PlaybackState.UNPREPARED)) {
|
||||||
noteBuilder.setProgress(playbackState.getDuration(), playbackState.getPlayedTime(), false);
|
noteBuilder.setProgress(playbackState.getDuration(), playbackState.getPlayedTime(), false);
|
||||||
noteBuilder.setIsPlaying(playbackState.isPlaying());
|
noteBuilder.setIsPlaying(playbackState.isPlaying());
|
||||||
|
@ -301,6 +300,8 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
|
||||||
};
|
};
|
||||||
|
|
||||||
private void afterPlayCleanup() {
|
private void afterPlayCleanup() {
|
||||||
|
// Notify thread to stop
|
||||||
|
setDonePlaying();
|
||||||
//remove progress bar
|
//remove progress bar
|
||||||
//noteBuilder.setProgress(0, 0, false);
|
//noteBuilder.setProgress(0, 0, false);
|
||||||
|
|
||||||
|
@ -314,7 +315,12 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
|
||||||
wifiLock.release();
|
wifiLock.release();
|
||||||
//remove foreground status of service; make BackgroundPlayer killable
|
//remove foreground status of service; make BackgroundPlayer killable
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
|
try {
|
||||||
|
// Wait for thread to stop
|
||||||
|
PlayerThread.this.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Log.e(TAG, "unable to join player thread", e);
|
||||||
|
}
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,9 +332,7 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCompletion(MediaPlayer mp) {
|
public void onCompletion(MediaPlayer mp) {
|
||||||
setDonePlaying();
|
|
||||||
afterPlayCleanup();
|
afterPlayCleanup();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue