Apps for Android Audio Focus (Requestaudiofocus)

Source: Internet
Author: User

Website: http://blog.csdn.net/dadoneo/article/details/8252933

From:http://www.linuxidc.com/linux/2012-04/57902.htm

================================================

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 in Android.media.AudioManager, get audiofocus use, requestAudioFocus() after use, give up audiofocus, use 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 continuity:
      • AUDIOFOCUS_GAINIndicates that the requested audio focus does not know how long it will last, generally a long-term possession;
      • AUDIOFOCUS_GAIN_TRANSIENTIndicates that the audiofocus to be applied is temporary and will soon be exhausted;
      • Audiofocus_gain_transient_may_duck not only said the application of the Audiofocus is temporary, but also indicates that you are currently using Audiofocus can continue to play, just to "duck" (lower 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 () method is audio Focus is preempted and again to be notified of the place. 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:

  1. Onaudiofocuschangelistener Afchangelistener = new Onaudiofocuschangelistener () {
  2. Public void onaudiofocuschange (int focuschange) {
  3. if (focuschange = = Audiomanager.audiofocus_loss_transient
  4. //Pause playback
  5. } Else if (focuschange = = Audiomanager.audiofocus_loss) {
  6. Am.unregistermediabuttoneventreceiver (Remotecontrolreceiver);
  7. Am.abandonaudiofocus (Afchangelistener);
  8. //Stop playback
  9. } Else if (focuschange = = Audiofocus_loss_transient_can_duck) {
  10. //Lower The volume
  11. } Else if (focuschange = = Audiomanager.audiofocus_gain) {
  12. //Resume playback or Raise it back to normal
  13. }
  14. }
  15. };

Iii. typical scenarios for application of Audiofocus

The following timing diagram depicts a typical scenario where Audiofocus is preempted and retrieved:

Audio focus is preempted and re-acquired time series diagram

Note : for the sake of simplicity, this figure, in addition to the two competing audio focus apps, only uses Audiomanager to characterize the objects inside the Android Audiofocus mechanism, The actual audiomanager is only the external representation, the internal participation of many objects, the callback function is not simply called directly by the Audiomanager, which also contains a complex IPC mechanism.

In the diagram:

    • Audiofocus client obtains audiofocus through Requestaudiofocus () and starts to play Audio[step#1 ~ #2] after acquiring Audiofocus;
    • Other programs (other apps) also get audiofocus of similar audiostream via Requestaudiofocus () [step#3]
    • Audiofocus client loses the audio Focus, in onaudiofocuschanged (), according to the value of Focuschange, to do the processing in the second section [step#4];
    • Other programs (other app) get audio focus and start playing audio[step#5];
    • Other program (other app) after using audio, by abandonAudioFocus() returning Audiofocus [step#6];
    • Audiofocus Client re-acquired audio Focus for further processing [step#7]

Summary

The Audio focus mechanism is to be fully understood and implemented uniformly by all parties, and there is a possibility of breaking this mechanism, resulting in a bad user experience, if there is a compliance or an error-making procedure exists. In the premise of ensuring that the built-in program is not a problem, if the procedure before entering Androidmarket is strictly carried out audiofocus related tests, should also be no problem.

Related reading: Audio playback in android: Controlling audio output channel switching http://www.linuxidc.com/Linux/2012-04/57901.htm

Problem points and further discussion

    • What is the internal adjudication mechanism?
    • Is there no competition between the different audio streams of the application?
Also refer to: http://wiki.eoeandroid.com/index.php?title=Managing_Audio_Focus&diff=12127&oldid=12075http:// blog.csdn.net/kesenhoo/article/details/7381985 Some of the macros used: http://developer.android.com/reference/android/media/ Audiomanager.html

Apps for Android Audio Focus (Requestaudiofocus)

Related Article

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.