flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

Duck sound of other apps when TTS is playing on Android. Audio Manager Focus

Open skvasov opened this issue 3 years ago • 8 comments

🚀 Feature Requests

Describe the feature

I work on navigation app. When the app announces the next manoeuvre I need to duck(temporary) sounds of all other apps. It works like a charm for iOS with _textToSpeech.setIosAudioCategory(IosTextToSpeechAudioCategory.playback, [IosTextToSpeechAudioCategoryOptions.duckOthers]), but there is not such feature for Android. As I know it can be done with Audio Manager Focus feature of Android.

Platforms affected (mark all that apply)

  • [ ] :iphone: iOS
  • [ x] :robot: Android

skvasov avatar Jan 19 '21 12:01 skvasov

Hi @dlutton, thanks for your useful plugin! Do you plan to work on this feature request in nearest future? We are about to release our app, so we would like to know how to proceed with this. Thanks!

skvasov avatar Jan 20 '21 10:01 skvasov

@skvasov sure, it doesn't seem overly complicated. I believe in your case you're looking to use https://developer.android.com/reference/android/media/AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

dlutton avatar Jan 20 '21 18:01 dlutton

@skvasov sure, it doesn't seem overly complicated. I believe in your case you're looking to use https://developer.android.com/reference/android/media/AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK

@dlutton right, AudioManager.requestAudioFocus() should be called somewhere before tts.speak() function. Thanks for quick reply! Waiting for update on this!

skvasov avatar Jan 20 '21 20:01 skvasov

Would it make sense to allow TTS to work nicely with the following plugin to help control such playback settings?

https://pub.dev/packages/audio_session

seanradford avatar Feb 15 '21 16:02 seanradford

@seanradford I agree it would be helpful to allow playback settings through that package, but we should also have the ability to do so without the assistance of another package.

dlutton avatar Feb 15 '21 20:02 dlutton

Just tried using audio_session and it does what is says, it announces to other audio players to duck (only tested with YT Music, Spotify on Android). Personally I am OK with doing this duck request explicitly through audio_session and not asking flutter_tts to include it inside speak(). Thanks for both teams for these packages!

This is how I used it:

final session = await AudioSession.instance;
flutterTts.setCompletionHandler(() {
    completionHandler?.call();
    session.setActive(false);
});
await session.setActive(true,  androidAudioFocusGainType:  AndroidAudioFocusGainType.gainTransientMayDuck);
await flutterTts.speak(str);

tamas-p avatar Mar 17 '21 17:03 tamas-p

@tamas-p that's great and thank you for the example. I still plan on adding a simple implementation of this to flutter_tts so you'll have the option either way.

dlutton avatar Mar 18 '21 06:03 dlutton

Just tried using audio_session and it does what is says, it announces to other audio players to duck (only tested with YT Music, Spotify on Android). Personally I am OK with doing this duck request explicitly through audio_session and not asking flutter_tts to include it inside speak(). Thanks for both teams for these packages!

This is how I used it:

final session = await AudioSession.instance;
flutterTts.setCompletionHandler(() {
    completionHandler?.call();
    session.setActive(false);
});
await session.setActive(true,  androidAudioFocusGainType:  AndroidAudioFocusGainType.gainTransientMayDuck);
await flutterTts.speak(str);

Thank you, that saved me hours or days. Hope that something similar finds its way into the TTS plugin. Thank you all for your work.

mindthefish avatar Mar 19 '21 14:03 mindthefish