Users often have a variety of options when using their Android device for multimedia interaction, most of which have a built-in microphone, a jack for a wired headset, and some that provide support for Bluetooth devices, A2DP support, and so on.
Check which device is being used
Your app should be affected by the audio playback device you're using to make a reasonable response.
The following code snippet shows that you can request Audiomanager to check if the audio playback is being directed to the device's horn, the wired headset, or the Bluetooth device.
1 2 if(Isbluetootha2dpon ()) {3 //Adjust output for Bluetooth.4 }elseif (Isspeakerphoneon ()) {5 //Adjust output for speakerphone.6 }elseif (Iswiredheadseton ()) {7 //Adjust output for headsets8}Else{9 //If audio plays and noone can hear it, is it still playing?Ten}
Response to audio output hardware changes
When you disconnect the headset and the Bluetooth headset, audio steam is automatically directed to the speaker, and if you use headphones, you are often used to using a larger volume, which can be very noisy after you dial the headset.
The Android system will issue a action_audio_becoming_noisy broadcast when the above situation occurs. It is best to register a broadcastreceiver in your activity regardless of whether you are listening to this intent while playing the audio. When playing music, users tend to want to pause when the headset is withdrawn, or to lower the volume of the game when the game is played.
1 2 privateclassnoisyaudiostreamreceiverextendsbroadcastreceiver{3 @Override4 publicvoid onreceive (Context context,intent Intent) {5 if(AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals (Intent.getaction ())) {6 //Pause the playback7 }8 }9 }Ten OnePrivateintentfilter Intentfilter =Newintentfilter (audiomanager.action_audio_becoming_noisy); A - privatevoid Startplayback () { - Registerreceiver (Mynoisyaudiostreamreceiver (), intentfilter); the } - - privatevoid Stopplayback () { - Unregisterreceiver (mynoisyaudiostreamreceiver); +}
3-Hardware to respond to audio output (managing audio Playback)