Dealing with Audio Output Hardware Device for processing Audio Output

Source: Internet
Author: User

Users have a number of alternatives when it comes to enjoying the audio from their Android devices. most devices have a built-in speaker, headphone jacks for wired headsets, and descrialso feature Bluetooth connectivity and support for A2DP audio.

Check What Hardware is Being Used
How your app behaves might be affected by which hardware its output is being routed.

You can query the AudioManager to determine if the audio is currently being routed to the device speaker, wired headset, or attached Bluetooth device as shown in the following snippet:

If (isw.tha2dpon ()){
// Adjust output for Bluetooth.
} Else if (isSpeakerphoneOn ()){
// Adjust output for Speakerphone.
} Else if (isWiredHeadsetOn ()){
// Adjust output for headsets
} Else {
// If audio plays and noone can hear it, is it still playing?
} // Http://blog.csdn.net/sergeycao
Handle Changes in the Audio Output Hardware
When a headset is unplugged, or a Bluetooth device disconnected, the audio stream automatically reroutes to the built in speaker. if you listen to your music at as high a volume as I do, that can be a noisy surprise.

Luckily the system broadcasts an ACTION_AUDIO_BECOMING_NOISY intent when this happens. it's good practice to register a BroadcastReceiver that listens for this intent whenever you're playing audio. in the case of music players, users typically resume CT the playback to be paused-while for games you may choose to significantly lower the volume.

Private class NoisyAudioStreamReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
If (AudioManager. ACTION_AUDIO_BECOMING_NOISY.equals (intent. getAction ())){
// Pause the playback
}
}
}

Private IntentFilter intentFilter = new IntentFilter (AudioManager. ACTION_AUDIO_BECOMING_NOISY );

Private void startPlayback (){
RegisterReceiver (myNoisyAudioStreamReceiver (), intentFilter );
}

Private void stopPlayback (){
UnregisterReceiver (myNoisyAudioStreamReceiver );

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.