How to switch the android music output device - android

I'm doing some test work on android music playback. I have a headset and a bluetooth headset connect with the phone at the same time.
Then there will be 4 output devices that can play the sound: speaker, handset, headset, and the BT_headset.
What I want to know is: is there a way to switch the audio output to the 4 device using code?
I cant do switch between speaker and handset if there is no headset plugin using audioManager.setMode(). But, if a headset plugin the sound can not out from handset.
Then, if the headset and bt_headset connect at the same time, the sound always out from the later connect one.

Related

Is it even possible to record from a device and play the recording on another device simultaneously?

Currently using AudioTrack and AudioRecord to record and play simultaneously, it's working great on the same device (either headset mic + headset speakers or phone mic + phone speakers). However, what I'd like to do is record from a device and play from another, for instance recording from my bluetooth headset microphone and then having the output on the device built-in speakers (or another pair of headset/headphones).
I have been trying for a few days but I could not find anything. Hence I am asking, is it even possible?

On Android 11, audio is not playing for other apps while my app is connected to a Bluetooth speaker mic

My application allows for users to transmit and receive audio, similar to a walkie talkie. Much like a walkie talkie, there is not always audio being transmitted/received, and we handle the audio focus as appropriate. Users are able to do this using both the phones built-in speaker + mic, and Bluetooth connected devices which provide speaker or mic. On Android 11, whenever my app is active while a Bluetooth device is connected, audio from other applications does not play.
As an example, say the user is using my app with a Bluetooth speaker mic connected. They are able to correctly receive (hear) my app audio through the Bluetooth device, and transmit (speak) audio through the Bluetooth device. However, if they attempt to play audio from another app, like say a video on YouTube - the video plays but the audio does not come through; either through the Bluetooth device or the phone speaker.
This incorrect behavior is new to my app on Android 11; audio works properly on prior versions of Android. Likewise, audio works properly when there is no connected Bluetooth audio device; other app audio plays as expected through the phone speaker when my app is not receiving/transmitting audio.
In my research into this issue, it appears there are some long-standing issues with Bluetooth on Android 11, with regards to both Android Auto and audio devices generally. My app utilizes the AudioManager SCO states to determine when an audio device is connected, and I suspect that a change in Android 11 related to this area is the culprit.
I haven't found many programmatic discussions about this issue, so if anyone has any ideas or suggestions about things to try, I would be most appreciative. Thanks for your time.

What is a proper way to route an audio from Android device to A2DP device

I'm new in android development, I'm developing a simple application that plays a music on button click through speaker , I would like to know how to route an audio to A2DP bluetooth device after paring with the A2DP device.
What are the configurations of AudioManager and other presets?
Thanks!

speech recognition through bluetooth headset in android while playing music

I am currently recognizing Hot word(like "ok google") using pocketsphinx library.
The app works fine except that it always uses only the phone's mic to recognize the speech.
My use case is something like this:
I listen for a hot word and use the MediaPlayer to play some music.
Keep listening for next commands while keeping the music being played and react accordingly.
The app works fine and the music is played via the bluetooth headset and the voice is also simultaneously recognized, but it always uses the phone's mic. Even though the bluetooth headset is connected or not connected, it still uses the phone's mic.
I tried using:
AudioManager mAudioManager =
(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.startBluetoothSco();
I tried using:
AudioManager mAudioManager =
(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
In both the approaches, it cuts off the music and listens in bluetooth mic.
I need an approach where the Music player is not cut from bluetooth headset but still I am able to speech recognize from bluetooth headset.
For example:
I was listening to music via bluetooth headset.
I opened voice recording app(https://play.google.com/store/apps/details?id=com.andrwq.recorder)
I am able to record the audio through bluetooth headset even while listening to music.
This makes me feel that it is possible but I don't know how.
Please help me out. Thanks in advance.
It is correct to start SCO mode, after that pocketsphinx will listen for bluetooth headset.
To disable phone microphone while in sco mode use am.setMicrophoneMute(true);
To play audio through SCO you need the player to play through STREAM_VOICE_CALL. Maybe player must be restarted after SCO switch.

Android Bluetooth Headset Transition

How exactly does Android handle the transition to a bluetooth headset when the connection is made? With the default music application, my device stops the audio from coming out the speaker, then routes it to the headphones as soon as a connection is made. When turning off the bluetooth headset, the device immediately detects the lost connection and starts routing through the device speaker again. Is this handled by the Android OS or by the application itself?
I ask because I am working on an app that plays streams off the internet. My app only behaves as described above 50% of the time. Sometimes when connecting the bluetooth headphones the audio does not automatically switch until I stop the output and start it again. The same goes for a disconnection. The device detects that the headphones are gone, but my audio does not automatically return back through the speakers until I restart the stream.

Categories

Resources