Reputation: 565
I have Text to Speech app and I'm wondering if there is a way to allow the user to listen to the TTS audio while they are listening to their music i.e. on spotify or an audio player.
At the moment the TTS plays over the top of spotify by default. Spotify doesn't stop when the tts starts, which is good, but, it is too loud.
Does anyone know if it's possible to lower the volume of spotify or other music that is playing when the user presses play on the TTS?
Upvotes: 2
Views: 1648
Reputation: 878
@cmd_prompter is right in that you're looking for the AudioManager on Android. (On iOS, the equivalent is the AVAudioSession.)
However, there is a Flutter package available for this use case now — the audio_session package.
The package allows you to determine how your app deals with background audio. You can ask other apps to "duck" their audio (= temporarily lower their volume) or to pause playback altogether.
Upvotes: 1
Reputation: 1680
This requires the usage of AudioManager
and AudioFocus
. Unfortunately there isn't any package published for flutter to communicate with the platform channel to control volume or request Audio focus and alternate volume programmatically, yet.
developer.android.com/guide/topics/media-apps/volume-and-earphones
> Controlling stream volume programmatically
In rare cases, you can set the volume of an audio stream programmatically. For example, when your app replaces an existing UI. This is not recommended because the Android AudioManager mixes all audio streams of the same type together. These methods change the volume of every app that uses the stream. Avoid using them:
adjustStreamVolume() adjustSuggestedStreamVolume() adjustVolume() setStreamVolume() setStreamVolume() setStreamSolo() setStreamMute()
About Audio Focus -
developer.android.com/guide/topics/media-apps/audio-focus
Managing audio focus
Two or more Android apps can play audio to the same output stream simultaneously. The system mixes everything together. While this is technically impressive, it can be very aggravating to a user. To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.
When your app needs to output audio, it should request audio focus. When it has focus, it can play sound. However, after you acquire audio focus you may not be able to keep it until you’re done playing. Another app can request focus, which preempts your hold on audio focus. If that happens your app should pause playing or lower its volume to let users hear the new audio source more easily.
Audio focus is cooperative. Apps are encouraged to comply with the audio focus guidelines, but the system does not enforce the rules. If an app wants to continue to play loudly even after losing audio focus, nothing can prevent that. This is a bad experience and there's a good chance that users will uninstall an app that misbehaves in this way.
Upvotes: 1