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,9 +113,17 @@ 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 (session.androidAudioAttributes.usage ==
AndroidAudioUsage.game) {
setVolume(volume / 2);
}
_playInterrupted = false;
break;
case AudioInterruptionType.pause:
case AudioInterruptionType.unknown:
if (playing) { if (playing) {
pause(); pause();
// Although pause is asyncand sets _playInterrupted = false, // Although pause is asyncand sets _playInterrupted = false,
@ -123,26 +131,21 @@ class AudioPlayer {
_playInterrupted = true; _playInterrupted = true;
} }
break; break;
case AudioInterruptionEvent.duck:
if (session.androidAudioAttributes.usage ==
AndroidAudioUsage.game) {
setVolume(volume / 2);
} }
_playInterrupted = false; } else {
break; switch (event.type) {
case AudioInterruptionEvent.end: case AudioInterruptionType.duck:
_playInterrupted = false;
break;
case AudioInterruptionEvent.resume:
if (_playInterrupted) play();
_playInterrupted = false;
break;
case AudioInterruptionEvent.unduck:
setVolume(min(1.0, volume * 2)); setVolume(min(1.0, volume * 2));
_playInterrupted = false; _playInterrupted = false;
break; break;
default: case AudioInterruptionType.pause:
if (_playInterrupted) play();
_playInterrupted = false;
break; break;
case AudioInterruptionType.unknown:
_playInterrupted = false;
break;
}
} }
}); });
}); });