TextToSpeech audio routed to phone speaker instead of bluetooth headset

1k views Asked by At

I have an app that uses android.speech.tts.TextToSpeech.speak() to readout messages.

The messages are only supposed to be readout to a connected bluetooth headset. So before I call the speak method I check AudioManager.isBluetoothA2dpOn();

Speak method implementation:

ttsOptions.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_VOICE_CALL);
ttsOptions.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, 1.0f);//MAX
tts.speak(text, TextToSpeech.QUEUE_ADD, ttsOptions, utteranceID);

This behaviour was working as expected for about a year but lately it fails on Samsung devices only in a way that it routes the audio to the phone's speaker and the message are read out loud.

I managed to fix the routing issue with a hack of:

1.AudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

2.mAudioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);

3.mAudioManager.startBluetoothSco();

4.Play a brief audio file

If I do these 4 steps the audio routing is fixed and routed to the bluetooth headset.

Questions:

Why do Samsung devices route the audio to the phone speaker instead of the bluetooth device?

Any reliable way to check whether the audio will be routed to the headset instead of the AudioManager.isBluetoothA2dpOn();?

0

There are 0 answers