-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
Comments
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? |
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 |
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. |
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!) |
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 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 This feature will obviously require some testing since it is a relatively significant change. |
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! |
Thanks! I'm really glad to hear this solves your problem and as you said hopefully other problems too. Best wishes. |
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. |
(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! 👍
The text was updated successfully, but these errors were encountered: