From de7da29ed6247d4676140e637b9d6f64afee9d8a Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Sun, 13 Sep 2020 19:47:14 +1000 Subject: [PATCH] Upgrade audio_session to handle willPauseWhenDucked correctly on Android. --- example/pubspec.lock | 4 +-- example/pubspec.yaml | 2 +- lib/just_audio.dart | 86 ++++++++++++++++++++++---------------------- pubspec.lock | 2 +- pubspec.yaml | 2 +- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index f643db1..8486d4d 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -14,7 +14,7 @@ packages: name: audio_session url: "https://pub.dartlang.org" source: hosted - version: "0.0.5" + version: "0.0.7" boolean_selector: dependency: transitive description: @@ -113,7 +113,7 @@ packages: path: ".." relative: true source: path - version: "0.4.2" + version: "0.4.3" matcher: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 7e4b97d..ef73479 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,7 +6,7 @@ environment: sdk: ">=2.7.0 <3.0.0" dependencies: - audio_session: ^0.0.5 + audio_session: ^0.0.7 flutter: sdk: flutter diff --git a/lib/just_audio.dart b/lib/just_audio.dart index 4dcd358..15d1915 100644 --- a/lib/just_audio.dart +++ b/lib/just_audio.dart @@ -115,49 +115,6 @@ class AudioPlayer { androidAudioSessionId: data['androidAudioSessionId'], ); //print("created event object with state: ${_playbackEvent.state}"); - if (handleInterruptions) { - AudioSession.instance.then((session) { - session.becomingNoisyEventStream.listen((_) { - pause(); - }); - session.interruptionEventStream.listen((event) { - if (event.begin) { - switch (event.type) { - case AudioInterruptionType.duck: - if (session.androidAudioAttributes.usage == - AndroidAudioUsage.game) { - setVolume(volume / 2); - } - _playInterrupted = false; - break; - case AudioInterruptionType.pause: - case AudioInterruptionType.unknown: - if (playing) { - pause(); - // Although pause is async and sets _playInterrupted = false, - // this is done in the sync portion. - _playInterrupted = true; - } - break; - } - } else { - switch (event.type) { - case AudioInterruptionType.duck: - setVolume(min(1.0, volume * 2)); - _playInterrupted = false; - break; - case AudioInterruptionType.pause: - if (_playInterrupted) play(); - _playInterrupted = false; - break; - case AudioInterruptionType.unknown: - _playInterrupted = false; - break; - } - } - }); - }); - } return _playbackEvent; } catch (e, stacktrace) { print("Error parsing event: $e"); @@ -216,6 +173,49 @@ class AudioPlayer { .distinct() .listen(setAndroidAudioAttributes); }); + if (handleInterruptions) { + AudioSession.instance.then((session) { + session.becomingNoisyEventStream.listen((_) { + pause(); + }); + session.interruptionEventStream.listen((event) { + if (event.begin) { + switch (event.type) { + case AudioInterruptionType.duck: + if (session.androidAudioAttributes.usage == + AndroidAudioUsage.game) { + setVolume(volume / 2); + } + _playInterrupted = false; + break; + case AudioInterruptionType.pause: + case AudioInterruptionType.unknown: + if (playing) { + pause(); + // Although pause is async and sets _playInterrupted = false, + // this is done in the sync portion. + _playInterrupted = true; + } + break; + } + } else { + switch (event.type) { + case AudioInterruptionType.duck: + setVolume(min(1.0, volume * 2)); + _playInterrupted = false; + break; + case AudioInterruptionType.pause: + if (_playInterrupted) play(); + _playInterrupted = false; + break; + case AudioInterruptionType.unknown: + _playInterrupted = false; + break; + } + } + }); + }); + } } /// The latest [PlaybackEvent]. diff --git a/pubspec.lock b/pubspec.lock index 629d408..20786c0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -14,7 +14,7 @@ packages: name: audio_session url: "https://pub.dartlang.org" source: hosted - version: "0.0.5" + version: "0.0.7" boolean_selector: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 41ffb2d..eec5b31 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ environment: flutter: ">=1.12.13+hotfix.5" dependencies: - audio_session: ^0.0.5 + audio_session: ^0.0.7 rxdart: ^0.24.1 path: ^1.6.4 path_provider: ^1.6.10