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