Merge pull request #3397 from ByteHamster/fix-crash-audio-focus-after-shutdown

Fixed receiving audio events after service shutdown
This commit is contained in:
H. Lehmann 2019-09-09 22:56:40 +02:00 committed by GitHub
commit 037c4d3922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -350,13 +350,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
setPlayerStatus(PlayerStatus.PAUSED, media, getPosition());
if (abandonFocus) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(audioFocusChangeListener);
audioManager.abandonAudioFocusRequest(builder.build());
} else {
audioManager.abandonAudioFocus(audioFocusChangeListener);
}
abandonAudioFocus();
pausedBecauseOfTransientAudiofocusLoss = false;
}
if (stream && reinit) {
@ -370,6 +364,16 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
});
}
private void abandonAudioFocus() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(audioFocusChangeListener);
audioManager.abandonAudioFocusRequest(builder.build());
} else {
audioManager.abandonAudioFocus(audioFocusChangeListener);
}
}
/**
* Prepares media player for playback if the service is in the INITALIZED
* state.
@ -701,6 +705,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
*/
@Override
public void shutdown() {
abandonAudioFocus();
executor.shutdown();
if (mediaPlayer != null) {
try {
@ -907,13 +912,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
mediaPlayer.reset();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioFocusRequest.Builder builder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(audioFocusChangeListener);
audioManager.abandonAudioFocusRequest(builder.build());
} else {
audioManager.abandonAudioFocus(audioFocusChangeListener);
}
abandonAudioFocus();
final Playable currentMedia = media;
Playable nextMedia = null;