Skip to content

Commit

Permalink
More options to control audio_session.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Feb 6, 2021
1 parent ae165cf commit d5079fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
7 changes: 0 additions & 7 deletions just_audio/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
fake_async:
dependency: transitive
description:
Expand Down
4 changes: 1 addition & 3 deletions just_audio/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ environment:
dependencies:
flutter:
sdk: flutter
audio_session: ^0.0.10
audio_session: ^0.0.11
rxdart: ">= 0.24.1 < 0.26.0"
just_audio:
path: ../

cupertino_icons: ^0.1.2

dev_dependencies:
flutter_driver:
sdk: flutter
Expand Down
48 changes: 32 additions & 16 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,26 @@ class AudioPlayer {
bool _automaticallyWaitsToMinimizeStalling = true;
bool _playInterrupted = false;
AndroidAudioAttributes _androidAudioAttributes;
bool _androidApplyAudioAttributes;
bool _handleAudioSessionActivation;

/// Creates an [AudioPlayer]. The player will automatically pause/duck and
/// resume/unduck when audio interruptions occur (e.g. a phone call) or when
/// headphones are unplugged. If you wish to handle audio interruptions
/// manually, set [handleInterruptions] to `false` and interface directly
/// with the audio session via the
/// [audio_session](https://pub.dev/packages/audio_session) package.
AudioPlayer({bool handleInterruptions = true}) : _id = _uuid.v4() {
/// manually, set [handleInterruptions] to `false` and interface directly with
/// the audio session via the
/// [audio_session](https://pub.dev/packages/audio_session) package. If you do
/// not wish just_audio to automatically activate the audio session when
/// playing audio, set [handleAudioSessionActivation] to `false`. If you do
/// not want just_audio to respect the global [AndroidAudioAttributes]
/// configured by audio_session, set [androidApplyAudioAttributes] to `false`.
AudioPlayer({
bool handleInterruptions = true,
bool androidApplyAudioAttributes = true,
bool handleAudioSessionActivation = true,
}) : _id = _uuid.v4(),
_androidApplyAudioAttributes = androidApplyAudioAttributes,
_handleAudioSessionActivation = handleAudioSessionActivation {
_idlePlatform = _IdleAudioPlayer(id: _id, sequenceStream: sequenceStream);
_playbackEventSubject.add(_playbackEvent = PlaybackEvent());
_processingStateSubject.addStream(playbackEventStream
Expand Down Expand Up @@ -151,13 +163,15 @@ class AudioPlayer {
_setPlatformActive(false);
_sequenceSubject.add(null);
// Respond to changes to AndroidAudioAttributes configuration.
AudioSession.instance.then((audioSession) {
audioSession.configurationStream
.map((conf) => conf?.androidAudioAttributes)
.where((attributes) => attributes != null)
.distinct()
.listen(setAndroidAudioAttributes);
});
if (androidApplyAudioAttributes) {
AudioSession.instance.then((audioSession) {
audioSession.configurationStream
.map((conf) => conf?.androidAudioAttributes)
.where((attributes) => attributes != null)
.distinct()
.listen(setAndroidAudioAttributes);
});
}
if (handleInterruptions) {
AudioSession.instance.then((session) {
session.becomingNoisyEventStream.listen((_) {
Expand Down Expand Up @@ -713,7 +727,7 @@ class AudioPlayer {
_playbackEventSubject.add(_playbackEvent);
final playCompleter = Completer();
final audioSession = await AudioSession.instance;
if (await audioSession.setActive(true)) {
if (!_handleAudioSessionActivation || await audioSession.setActive(true)) {
// TODO: rewrite this to more cleanly handle simultaneous load/play
// requests which each may result in platform play requests.
final requireActive = _audioSource != null;
Expand Down Expand Up @@ -991,10 +1005,12 @@ class AudioPlayer {
final playing = this.playing;
// To avoid a glitch in ExoPlayer, ensure that any requested audio
// attributes are set before loading the audio source.
final audioSession = await AudioSession.instance;
if (_androidAudioAttributes == null) {
_androidAudioAttributes =
audioSession.configuration?.androidAudioAttributes;
if (_androidApplyAudioAttributes) {
final audioSession = await AudioSession.instance;
if (_androidAudioAttributes == null) {
_androidAudioAttributes =
audioSession.configuration?.androidAudioAttributes;
}
}
if (_androidAudioAttributes != null) {
await _internalSetAndroidAudioAttributes(
Expand Down
2 changes: 1 addition & 1 deletion just_audio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
just_audio_platform_interface: ^2.0.0
just_audio_web: ^0.2.3
audio_session: ^0.0.10
audio_session: ^0.0.11
rxdart: ">= 0.24.1 < 0.26.0"
path: ^1.6.4
path_provider: ^1.6.10
Expand Down

0 comments on commit d5079fb

Please sign in to comment.