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();?
Related
As we know,we could record voice via Bluetooth headset,we should use SCO link, but when a call coming, the system will lost sco link,so that, I can't get PCM data from SCO link. how to resolve it.
I'd try to this code,but it doesn't work.
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.startBluetoothSco();
am.setBluetoothScoOn(true);
when phone is ringing, can i record from SCO?
Android changes audio routing depending on its needs, and when the phone is ringing (and/or when the call is answered) the audio is re-routed to target device (earpiece, headphones, BT, etc..). It's impossible to start a single recording Object and get all the data (the data before, current and future voice during communication), but you have to monitoring the voicecall status (or the current Audio Routing state) and close the previous recording object and then create a new one using different settings.
If you are interested ONLY in your voice while your phone is ringing, you could do it by recording that data using UPLINK as the AudioSource of Recording object.
I am able to receive, hangup and dial a number from my headset to android mobile which are connected via bluetooth.
Now, once I receive a in-coming call of mobile in headset, I need to speak with the called person from headset. I need to stream my voice via bluetooth to android mobile. I am searching for this code.
Crawled n number of post but could not get appropriate answer, Currently am using below but its not working...
AudioManager audioManager;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
audioManager.setSpeakerphoneOn(true);
I am not sure after this what line of code I need to write to achieve my requirement
Note: I receive in-coming call by sending mmOutStream.write("ATA\r".getBytes()); this AT command to my Android mobile.
Please correct and guide me to perform audio stream.
You should set speakerphone to false, otherwise your output will be re-directed there. It is enough to do this:
audioManager.setSpeakerphoneOn(false);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
Also, you can optionally set microphone to un-mute state (just in case previous state was muted):
mAudioManager.setMicrophoneMute(false);
I am trying to identify how to route a very short audio stream (a notification) to a bluetooth headphone that is already paired with the device, while the device is ringing.
When I play any audio at any time, it is routed to the bluetooth device, no problem.
But if I try to start playing the audio when receiving an android.intent.action.PHONE_STATE, in RINGING state, the audio is not routed as expected.
I can see that the AudioManager's setBluetoothA2dpOn method has became deprecated, but I actually tried it but is seems has no effect.
I have tried the MediaRouter object, but I can see that MediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO) points to the RouteInfo of the Bluetooth device while the device is ringing, and the AudioManager.isBluetoothA2dpOn is true.
So, can any one tell me why the audio route is like this in the ringing moment? is there any way to force the audio to be routed to the Bluetooth device in such case?
[UPDATED]
I have tried again today and I have discovered something that may be the cause of the problem.
I have created a BroadcaseReceiver to detect the change in the android.intent.action.PHONE_STATE. if an intent is received and the state is currently ringing, check for AudioManager's mode and you will find it is MODE_NORMAL. but few seconds later the phone will start actually ringing and the mode is going to be changed into MODE_RINGTONE. trying to manually set the mode using the method setMode(AudioManager.MODE_NORMAL) is useless then, the state remains MODE_RINGTONE even after setting it to MODE_NORMAL.
Now, I think the cause of the problem is that in the MODE_RINGTONE mode, all the streams are directed to the phone speaker and here there is no way offered by the android system to change the mode.
I think the Media player realease the bluetooth connection when the phone is ringing. You can try to obtain the bluetooth audio connection and see if Media player now play through your obtained connection. You can use my class at my answer Using the Android RecognizerIntent with a bluetooth headset and see if it works. The audio in the class is Sco only.
As stated in the JavaDoc for the StartBluetoothSco method:
Note that the phone application always has the priority on the usage of the SCO connection for telephony. If this method is called while the phone is in call it will be ignored. Similarly, if a call is received or sent while an application is using the SCO connection, the connection will be lost for the application and NOT returned automatically when the call ends.`
I tried to start sco then play a music clip in normal mode, it played to the Bluetooth headset without problems, although I couldnot stop the microphone that caused the input stream plays to the headset. I then tried to call my target phone from another phone while the music is still playing, I found that the stream has got redirected automatically to the phone speaker. After the ringing mode is finished, the stream did not get redirected again to the Bluetooth headset and I think that behavior is normal according to what is stated in the JavaDoc above.
My guessing is that Android tries to protect the ringing and in-call modes as possible in order not to allow any unwanted interference from applications. In other words, when in ringing mode then no sound is going to be played to the headset until the call is accepted, and you cannot even change the AudioManager mode from ringing to another mode, your call for mode setter will be ignored.
I have tried the AudioTrack instead of MediaPlayer, but that makes no difference.
I have then tried the TextToSpeech engine like this:
in the main activity, initialize on create:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech=new TextToSpeech(this, this);
textToSpeech.setLanguage(Locale.US);
textToSpeech.addEarcon("[wwww]", "XXXXXXXXXXXXXXXXXXXXXXX", R.raw.a);
}
in the broadcast receiver class when rining starting the Bluetooth utility class and adding the below to the onScoAudioConnected method
textToSpeech.playEarcon("[wwww]", TextToSpeech.QUEUE_FLUSH, null);
This did not work too.
There is a problem when using Google Navigation on Android with a Bluetooth device. Navigation sends the audio for the turn-by-turn instructions over the A2DP stream. I have a Motorola T605 Bluetooth car kit and it supports HFP and A2DP. I often just listen to the old fashioned car radio though. In this scenario I never hear turn-by-turn directions because my live A2DP stream is being sent to the radio AUX input but I am listening to the radio instead. My app has a feature where it reads out SMS messages to me. I have it set up so you can pick the stream you want to use for this. I prefer using AudioManager.STREAM_VOICE_CALL since it uses a dedicated separate speaker on the T605.
I have looked into a few ways to handle this:
1) Reroute notifications to Bluetooth SCO instead of A2DP. I have not fund a way to do this. I am able to send TTS messages I create over this path and it works great (I have an SMS reader built into my app). I tried activating SCO but the turn-by-turn instructions stream moves to the phone internal speaker instead of the voice call stream like I would prefer.
2) Capture the navigation messages and echo them back over the SCO path. I can't find a way to intercept or capture the navigation stream though. Does Google Navigation use the TTS engine or its own methods to create the voice output? It would be great to just get the directions in a text string since I could easily send that to the TTS engine and route it where I want like I do with SMS strings.
You can see my app source here: http://code.google.com/p/a2dpvolume/
Any thoughts?
Unfortunately I think you're SOOL when it comes to rerouting the navigation messages to BT SCO. I would expect the navigation app to use either the TTS or NOTIFICATION stream type to play the messages, and both of those stream types follow the MEDIA routing strategy (for NOTIFICATIONs that is at least typically true when there's no active voice call).
Although the behavior is up to each vendor to decide, my guess is that you'll find that in most implementations streams that follow the MEDIA routing strategy won't ever be routed to BT SCO, except during an ongoing voice call that is routed to BT SCO.
The best you could do in terms of routing is to force the routing to the loudspeaker, or to "anything but A2DP" (which means wired headset if one is attached, or the loudspeaker otherwise).
Here's how you could do that (I haven't verified that this works on every phone out there):
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse",
int.class,
int.class);
// 1 == FOR_MEDIA, 10 == FORCE_NO_BT_A2DP (FORCE_SPEAKER would be 1).
setForceUse.invoke(null, 1, 10);
I'm trying to play two files at the same time and route one to wired headset and the other to the BT headset. Is this even possible? Any ideas how I can achieve this? I'm targeting OS 2.3 and greater.
You can try creating two MediaPlayers with different stream types:
btPlayer = new MediaPlayer(...);
wiredPlayer = new MediaPlayer(...);
...
btPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
wiredPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
...
Of cause you'll need to put extra code to redirect btPlayer to bluetooth headset. For this you'll need to use startBluetoothSco() and setBluetoothScoOn().
Also note, that audio can be redirected to bluetooth headset only on AudioManager.STREAM_VOICE_CALL. But if you are using a2dp bluetooth device, you can redirect audio to this device in AudioManager.STREAM_MUSIC stream as well.