Android music programming: Manages audio focus

Source: Internet
Author: User

Before you start playing an audio file, your application must first obtain the audio focus and register a notification of loss of the listening audio focus at the same time, that is, if the audio focus is preemptible by the system or other applications, your application can make the appropriate response.

Obtain audio focus

Your application should first hold the audio focus of the stream before it starts playing any audio.

You can call requestAudioFocus (). If the request is successful, AUDIOFOCUS_REQUEST_GRANTED is returned.

You must specify which stream is used for the stream type) and the type of audio focus is short or persistent ).

Transient focus is used to play audio for a short period of time, for example, playing navigation commands ).

Persistent focus is used to play audio for a long period of time, for example, playing music ).

The following code snippet demonstrates the permanent audio focus that requests a music audio stream.

For example, when you press the playback key of the background music, you should request the audio focus immediately and start playing the music after the focus is obtained.

 
  1. AudioManager am = mContext. getSystemService (Context. AUDIO_SERVICE );
  2. ...
  3.  
  4. // Request audio focus for playback
  5. Int result = am. requestAudioFocus (afChangeListener,
  6. // Use the music stream.
  7. AudioManager. STREAM_MUSIC,
  8. // Request permanent focus.
  9. AudioManager. AUDIOFOCUS_GAIN );
  10.  
  11. If (result = AudioManager. AUDIOFOCUS_REQUEST_GRANTED ){
  12. Am. unregisterMediaButtonEventReceiver (remotecontrolpolicer );
  13. // Start playback.
  14. // Start playing the music file
  15. }

Once your application finishes playing, remember to call the abandonAudioFocus () method to notify the system to release the audio focus and cancel the relevant AudioManager. OnAudioFocusChangeListener. In this way, when your application releases the audio focus, the system will allow other interrupted applications to obtain the focus again to continue playing.

 
  1. // Abandon audio focus when playback complete 
  2. am.abandonAudioFocus(afChangeListener); 

When a transient audio focus is requested, an additional parameter is provided to specify whether "DUCK" is allowed ". Normally, the playback should stop when the application loses the audio focus. If you set the "DUCK" additional parameter when obtaining the Short audio focus, you can allow other applications to continue playing the video without stopping it. You only need to reduce the volume, then, when your application releases the focus and other applications obtain the focus again, the volume is restored to the initial state.

 
  1. // Request audio focus for playback 
  2. int result = am.requestAudioFocus(afChangeListener, 
  3. // Use the music stream. 
  4. AudioManager.STREAM_MUSIC, 
  5. // Request permanent focus. 
  6. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); 
  7.  
  8. if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
  9.   // Start playback. 
  10. } 

"DUCK" is especially suitable for applications that play audio streams intermittently, such as sound prompts for driving navigation.

When other applications request a permanent, short, and DUCK audio focus, the audio focus listener registered in your application receives a Message notification.

Processing loss of audio focus

If your application can request the audio focus, your application may also lose the audio focus when other applications apply. How to deal with the loss of audio focus is determined by the loss of audio focus.

When the audio focus is lost, the listener function onAudioFocusChange () You registered will receive an event notification. The parameters in the notification include specific information, such as permanent focus loss and Short Focus loss, it is also a transient focus and DUCK's focus is allowed to be lost.

In general, if the focus is lost for a short time, your application should pause the current playback, but the other status information will continue to be maintained, and you should continuously listen for changes in the audio focus, once you obtain the audio focus again, you can immediately restore it to the previous playback status.

If it is the loss of permanent focus, it usually means that other applications have to play the audio for a long time. Your application should stop playing the audio and discard the listening of the audio focus, and cancel all media button listeners, so that other applications with the focus can listen to the key messages of the media playback control button. To restore the playback status of your application, you usually need to restart your application.

In the following code snippet, if a short audio focus is lost, the playback of the current application is tentative. If a permanent audio focus is lost, the playback of the current application is stopped, stop listening for changes to the audio focus and deregister the media button event.

 
  1. OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { 
  2.   public void onAudioFocusChange(int focusChange) { 
  3.     if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT 
  4.        // Pause playback 
  5.     } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { 
  6.       // Resume playback 
  7.     } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { 
  8.       am.unregisterMediaButtonEventReceiver(RemoteControlReceiver); 
  9.       am.abandonAudioFocus(afChangeListener); 
  10.       // Stop playback 
  11.     } 
  12.   } 
  13. }; 

If the lost transient audio focus allows the DUCK State, in this case, the application reduces the volume to continue playing, and does not need to pause.

Duck!

DUCK: reduces the volume of your app and does not disturb the playback of audio from other apps.

In the following code snippet, when we lose the focus, we reduce the media playback volume and resume the focus to the original state.

 
  1. OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { 
  2.   public void onAudioFocusChange(int focusChange) { 
  3.     if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK 
  4.       // Lower the volume 
  5.     } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { 
  6.       // Raise it back to normal 
  7.     } 
  8.   } 
  9. }; 

The loss of audio focus is a very important notification of system status changes. The system will broadcast many notifications about audio status changes. Your application should listen for these changes, and make appropriate solutions to improve the user's music experience.

Reference Abstract:

Http://developer.android.com/training/managing-audio/audio-focus.html

Original article:

Http://blog.zhourunsheng.com/2011/12/android-%E9%9F%B3%E4%B9%90%E7%BC%96%E7%A8%8B%E4%B8%93%E9%A2%98%E4%B9%8B%E7% AE %A1%E7%90%86%E9%9F%B3%E9%A2%91%E7%84%A6%E7%82%B9/

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.