Audio Playback in Android: control audio output channel Switching

Source: Internet
Author: User

There are many audio output channels, such as Speaker, headset, and Bluetooth a2dp. The audio output channel may be switched during calls or playing music. For example, when a wired headset is inserted to play music, the sound is sent from the headset. When the headset is pulled out, the audio output channel switches. If the player does not process the audio, the audio output is switched to the speaker, and the sound is directly sent from the speaker. When writing a program, we need to capture and handle such a thing as needed. This article explains how to deal with it.

In Android, you can use Android. Media. audiomanager to query the current audio output. When the audio output changes, the changes are captured and processed.

I. query and control the output status of audio

Android. Media. audiomanager provides the following methods to query the current audio output status:

  •  Isw.tha2dpon (): Check whether a2dpaudio Uses Bluetooth headsets;
  •  Isspeakerphoneon (): Check whether the speaker is on;
  •  Iswiredheadseton (): Check whether the headset is connected. Note that this method is only used to determine whether the headset is inserted. It cannot be used to determine whether the current audio is output through the headset, this also depends on other conditions.

In addition, there are some setxyz () methods for setting these audio outputs. These methods are generally used in audio output applications.NoDirect calls are managed by the system to automatically switch the audio output channel. Unless the interface provides a menu or button for User Switching, but the user selects but changes it. For example, if you want to directly select the speaker to speak, you can directly call setspeakerphoneon ().

Ii. Capture and process events for audio output channel Switching

The audio output path is automatically switched because the headset is plugged in and out, and the Bluetooth headset is disconnected. At this time, the program playing audio needs to be notified about the event. In Android, intent notifications are broadcast through action_audio_becoming_noisy.

A better way to handle broadcasts is to dynamically register/deregister broadcasts of interest to you. The following code demonstrates registering a broadcaster when starting playing a broadcast; canceling a broadcaster when stopping playing a broadcast. The process for switching the audio output channel is to pause the current playback and do not directly generate a sound from the new channel.

 
 
  1. private class NoisyAudioStreamReceiver extends BroadcastReceiver {  
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.         if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {  
  5.             // Pause the playback   
  6.         }  
  7.     }  
  8. }  
  9.   
  10. private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);  
  11.   
  12. private void startPlayback() {  
  13.     registerReceiver(myNoisyAudioStreamReceiver(), intentFilter);  
  14. }  
  15.   
  16. private void stopPlayback() {  
  17.     unregisterReceiver(myNoisyAudioStreamReceiver);  
  18. }   

Iii. Typical scenarios of audio output channel switching-unplug the headset when listening to music

When listening to music from headphones, do not pull out the sequence diagram as follows:

Figure:

  • Audionoisy client registers audiomanager. action_audio_becoming_noisy [step #1 ~ #2];
  • I have been listening to music with headphones;
  • Headsetobserver keeps monitoring changes in the headset status. After the headset is removed, broadcast audiomanager. action_audio_becoming_noisy [step #3 ~ 4];
  • The audionoisy client receives the broadcast and sends the pause command to mediapaybackservice to pause the current playback. [step #5 ~ 6].

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.