3-Hardware to respond to audio output (managing audio Playback)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.