Android WebView Audio off issue

Source: Internet
Author: User

About WebView loading of the Web page to load some audio files, exiting when unable to close the problem, a bunch of solutions on the net, but did not solve my problem. Our interface is a very special case, in the ondestory, can not immediately close WebView (Mywebview.destroy (); mywebview = null;)

Later all kinds of attempts, can not close, finally thought of using the system of audio management to deal with this problem, did not expect to really can, the following directly put code:

if (Audiomanager = = null) {                Audiomanager = (audiomanager) getsystemservice (Context.audio_service);            }            /** System Audio CONTROL permission, Audiofocus_gain_transient briefly gets **/            int result = Audiomanager.requestaudiofocus (                    Adfocuschangelistener, Audiomanager.stream_music, audiomanager.audiofocus_gain_transient);            The/**audiofocus_request_granted ID gets control and then the listener is released **/            if (result = = audiomanager.audiofocus_request_granted) {                if (Audiomanager! = null) {                    audiomanager.abandonaudiofocus (adfocuschangelistener);                }            }

  

    /**     * Get audio focus     **/    private Audiomanager.onaudiofocuschangelistener Adfocuschangelistener = new Audiomanager.onaudiofocuschangelistener () {public        void onaudiofocuschange (int focuschange) {        }    };

  

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.

Android WebView Audio off issue

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.