Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(NEW API - TESTERS NEEDED) Option to get audio focus that will duck others #416

Closed
m-tay opened this issue Aug 10, 2020 · 8 comments
Closed
Assignees
Labels
3 testing enhancement New feature or request

Comments

@m-tay
Copy link

m-tay commented Aug 10, 2020

(edit by @ryanheise) TLDR, Apps can (or "should" ;-) ) now use audio_session to initialise the app's audio session category and options during app initialisation. e.g. audioSession.configure(AudioSessionConfiguration.music()); for a music app. Audio interruptions can now be automatically handled by just_audio, although you can also choose to handle it yourself by interfacing with audio_session. The latest example on git master shows how to use audio_session, and testing/feedback would be greatly appreciated.

Now onto the original poster's feature request...


The plugin works great, but I cannot see any way of setting how you deal with audio focus. My use case is a running coach app, so I would like to play short audio clips that temporarily ducks all the other audio sources. As far as I can see, when using setState to set playing = true, this stops all the other audio sources, but they don't play when the audio has finished.

Android/iOS provide different ways of getting audio focus, so other apps know whether to stop or duck audio. It would be great of audio_service could expose that functionality too (unless I've totally missed something!)

I can't think of any other way to approach this. For my use case, audio ducking is pretty important. Also wasn't sure if this should be dealt with in audio_service or just_audio. Thanks! 👍

@m-tay m-tay added 1 backlog enhancement New feature or request labels Aug 10, 2020
@ryanheise
Copy link
Owner

That's interesting, I had never thought about this use case. Yes, it should be handled in audio_service since this plugin manages the audio focus.

I can see what you would like on Android, although I'm not sure about iOS. Do you know the specific underlying API you want to set, if any?

@m-tay
Copy link
Author

m-tay commented Aug 11, 2020

Thanks for responding! I believe on iOS that setting this category option on AVAudioSession would do the trick: https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions/1616618-duckothers

@ryanheise
Copy link
Owner

I see, so presumably you would be happy with the existing API to set the category option, and I can just make an Android-specific option.

@m-tay
Copy link
Author

m-tay commented Aug 12, 2020

Yeah, access to the existing API would be fine. Android appears to have a similar feature, described here https://developer.android.com/guide/topics/media-apps/audio-focus where you can request focus with the AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK flag. The use case described for this flag is the use case I have - short audio clips where you want other audio sources to duck instead of stop. (You may have already seen all that, but I thought I would mention it for completeness!)

@ryanheise
Copy link
Owner

ryanheise commented Aug 23, 2020

Hi @m-tay apologies for not giving you an update until now, but I have been busy working on this. I actually took the opportunity to rethink the way audio focus is handled in general because currently the fact that audio focus is gained internally provides you no way to know whether the request failed, which is important since an app should not continue to play audio in this case. There is also another issue where apps using multiple audio plugins for various purposes may be surprised to find that those plugins will overwrite each other's audio session category and other options whereas this should be a property of the app rather than a property of any one plugin.

For this and other reasons, I decided that the way to go would be to create a new plugin to manage both your AVAudioSession and your Android AudioManager. This new plugin is called audio_session.

I have created a new branch called audio-session on audio_service and also similarly on just_audio to demonstrate how to use it.

The main impact on audio_service is that it is no longer audio_service's responsibility to manage these audio focus events, and the ability to set the iOS category and options has also been removed. It didn't really make sense for every plugin to have their own version of settings the iOS category, so I also removed this option from just_audio, too.

Now, what you would do in your app's initialisation (for your specific use case) is:

final session = await AudioSession.instance;
await session.configure(AudioSessionConfiguration(
  avAudioSessionCategory: AVAudioSessionCategory.playback,
  avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.duckOthers,
  avAudioSessionMode: AVAudioSessionMode.spokenAudio,
  androidAudioAttributes: const AndroidAudioAttributes(
    contentType: AndroidAudioContentType.speech,
    usage: AndroidAudioUsage.voiceCommunication,
  ),
  androidAudioFocusGainType: AndroidAudioFocusGainType.gainTransientMayDuck,
));

Not sure on the best AndroidAudioUsage value, assistant also seems like it might be a valid choice even though this is not for Google's assistant.

This feature will obviously require some testing since it is a relatively significant change.

@ryanheise ryanheise pinned this issue Aug 23, 2020
@ryanheise ryanheise changed the title Option to get audio focus that will duck others (NEW API - TESTERS NEEDED) Option to get audio focus that will duck others Aug 23, 2020
@m-tay
Copy link
Author

m-tay commented Sep 5, 2020

Hi @ryanheise - this is amazing, thanks so much! Was not expecting such a... comprehensive response. I've updated all the plugins and your suggested implementation works perfectly - the audio is now ducking and returning as desired. Great work, this is so helpful and will surely benefit many others in the Flutter ecosystem too!

@ryanheise
Copy link
Owner

Thanks! I'm really glad to hear this solves your problem and as you said hopefully other problems too. Best wishes.

@ryanheise ryanheise unpinned this issue Sep 7, 2020
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 testing enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants