Android is a multitasking system and the audio system is a competitive resource. Prior to Android2.2, there was no built-in mechanism to solve the problem of multiple programs competing for audio, and 2.2 introduced a mechanism called Audiofocus to manage and coordinate the competition for audio resources. This article mainly explains the use of Audiofocus.
In accordance with the Audiofocus mechanism, before using audiostream, you need to apply for Audiofocus, you can use the corresponding Audiostream after acquiring Audiofocus, if there are other programs competing for the audiostream you are using, Your program needs to stop playing or reduce the processing of the sound after receiving the notification. It is worth noting that this mechanism needs to be co-operative and that all programs that use audio resources are done in accordance with this mechanism, and that if a program is still using Audio,audiofocus when it loses its audiofocus, it cannot be done. This is deadly for open systems Android: Users may install programs that do not follow this mechanism, or programs that are too old to introduce this mechanism, which can ultimately lead to poor user experience.
For mobile solutions companies, the thing to do is educate and train team members to ensure that their built-in program compliance mechanisms are no problem, including Android native programs, their own programs, and programs that fit third parties.
I. Application and release of Audiofocus
Here's a look at the classes related to Audiofocus:
get/Discard Audiofocus methods are all in Android.media.AudioManager, get Audiofocus with requestaudiofocus () ; After use, discard Audiofocus, with abandonaudiofocus () .
Among them, parameters :
- Streamtype is the Audiostream described in "audio playback in Android: volume and Remote Playback control", the value of which depends on the stream_xxx in Audiomanager;
- Durationhint is an indication of persistence:
audiofocus _gain indicates that the requested audio focus does not know how long it will last, generally a long-term possession;
audiofocus _gain_transient indicates that the audiofocus to be applied is temporary and will soon be exhausted;
- audiofocus_gain_transient_may_duck not only that the audiofocus to be applied is temporary, Also indicates that you are currently using Audiofocus to continue playing, just "duck" (lower the volume).
- Audiomanager.onaudiofocuschangelistener is to monitor the use of Audiofocus after the successful application of listener, follow-up if there are other programs to compete Audiofocus, This listener Onaudiofocuschange () method is used to inform the user of this audio focus.
The return value , which may be:
- audiofocus_request_granted: Successful application;
- audiofocus_request_failed: The application failed.
Ii. Audiofocus being preempted and re-acquired
As we know from the previous section, the audiomanager.onaudiofocuschangelistener parameter is passed in when the Audiofocus is requested/released, and its onaudiofocuschange () The method is where audio focus is preempted and again notified. Therefore, each program to use Audiofocus should be careful to implement this function, to ensure the consistency of audiofocus implementation.
The Focuschange parameter of the Onaudiofocuschange () method indicates the owner of the Audiofocus competitor to Audiofocus, with the following values:
- Audiofocus_gain: acquired the audio Focus;
- Audiofocus_loss: Lost audio Focus and it will last for a long time. This is because it may stop for a long time, so not only to stop audio playback, it is best to release media resources directly. And because the time to stop playing audio will be very long, if the program for this reason and lost Audiofocus, it is best not to let it automatically get audiofocus and continue to play, or suddenly come out of the voice will make users feel puzzled, feel very bad. There is no need to listen to the remote playback control "like the processing of the code below", Audiofocus directly. To play again, the media will not be re-initialized until the user clicks on the screen to start playing again.
- audiofocus_loss_transient: Temporarily loses audio Focus and will be available again soon. Audio playback must be stopped, but it is possible to get Audiofocus again soon, where media resources are not released;
AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: Temporarily loses audiofocus, but can continue playing, but to lower the volume.
Here is the code snippet processed by the Onaudiofocuschange () method:
- Onaudiofocuschangelistener Afchangelistener = new Onaudiofocuschangelistener () {
- Public void onaudiofocuschange (int focuschange) {
- if (focuschange = = Audiomanager.audiofocus_loss_transient
- //Pause playback
- } Else if (focuschange = = Audiomanager.audiofocus_loss) {
- Am.unregistermediabuttoneventreceiver (Remotecontrolreceiver);
- Am.abandonaudiofocus (Afchangelistener);
- //Stop playback
- } Else if (focuschange = = Audiofocus_loss_transient_can_duck) {
- //Lower The volume
- } Else if (focuschange = = Audiomanager.audiofocus_gain) {
- //Resume playback or Raise it back to normal
- }
- }
- };
==========================================================================
From:http://www.linuxidc.com/linux/2012-04/57902.htm
Android Audiomanager Get Media focus