Chaphasilor
Chaphasilor

Reputation: 51

How can I prevent starting playback if the phone speaker will be used with audio_session in Flutter?

I'm currently implementing a "resume on bluetooth speaker/headphone" connect in a Flutter app using audio_session. I'm using the AudioDevicesChangedEvent to get notified about newly connected devices, and then check if their type is AudioDeviceType.bluetoothSco. That was the closest type I could find to what I'm looking for, there doesn't seem to be a device for "anything that can output audio over bluetooth". There is AudioDeviceType.bluetoothA2dp, but that seems to require that the device supports the A2DP protocol, which I assume not all bluetooth speakers do.
I also check that the devices is an output device (isOutput == true).

The problem now is that there are some devices, like my laptop, that will match these criteria and prompt playback start, even though the phone doesn't use them as an output device, resulting in music being played over the phone's loud speaker, which I don't want to happen.

I tried somehow getting info about which device is currently being used as the output, or if the newly connected device will be used as the output device, but couldn't find a way to get that info in audio_session.
I also found the becomingNoisyEventStream, but that (also according to Android docs) can only be used to stop playback when a bluetooth device is disconnected, but cannot be used as a way to check if the playback would currently be noisy. This is because this is a type-less stream which only gives an event if the output becomes noisy, but not if it becomes silent (so there's no way to keep track of what the current noise level is).

Is there any other way to prevent playing music over the loudspeaker?

Thanks in advance!

Upvotes: 0

Views: 70

Answers (0)

Related Questions