Use new audio focus API.

This commit is contained in:
Ryan Heise 2020-08-21 14:09:24 +10:00
parent eae3c1b79b
commit 82b6d2ac8b
1 changed files with 33 additions and 30 deletions

View File

@ -113,36 +113,39 @@ class AudioPlayer {
if (handleInterruptions) { if (handleInterruptions) {
AudioSession.instance.then((session) { AudioSession.instance.then((session) {
session.interruptionEventStream.listen((event) { session.interruptionEventStream.listen((event) {
switch (event) { if (event.begin) {
case AudioInterruptionEvent.pauseIndefinitely: switch (event.type) {
case AudioInterruptionEvent.pauseTemporarily: case AudioInterruptionType.duck:
if (playing) { if (session.androidAudioAttributes.usage ==
pause(); AndroidAudioUsage.game) {
// Although pause is asyncand sets _playInterrupted = false, setVolume(volume / 2);
// this is done in the sync portion. }
_playInterrupted = true; _playInterrupted = false;
} break;
break; case AudioInterruptionType.pause:
case AudioInterruptionEvent.duck: case AudioInterruptionType.unknown:
if (session.androidAudioAttributes.usage == if (playing) {
AndroidAudioUsage.game) { pause();
setVolume(volume / 2); // Although pause is asyncand sets _playInterrupted = false,
} // this is done in the sync portion.
_playInterrupted = false; _playInterrupted = true;
break; }
case AudioInterruptionEvent.end: break;
_playInterrupted = false; }
break; } else {
case AudioInterruptionEvent.resume: switch (event.type) {
if (_playInterrupted) play(); case AudioInterruptionType.duck:
_playInterrupted = false; setVolume(min(1.0, volume * 2));
break; _playInterrupted = false;
case AudioInterruptionEvent.unduck: break;
setVolume(min(1.0, volume * 2)); case AudioInterruptionType.pause:
_playInterrupted = false; if (_playInterrupted) play();
break; _playInterrupted = false;
default: break;
break; case AudioInterruptionType.unknown:
_playInterrupted = false;
break;
}
} }
}); });
}); });