Android often uses WebView to load the H5 page. If there is audio or video playing in the H5 page and you exit the interface before finishing playing, you will find that the audio or video is still playing in the background, which is a little confused. Here is the solution:

Solution a:

Write two sentences in the onPause() and onResume() methods of the activity in which the WebView is located. public voidonPause() {
        super.onPause();  
        webview.onPause();  
    }  
public void onResume() {
        super.onResume();  
        webview.onResume();  
    }  
Copy the code

Scheme 2:

Private AudioManager AudioManager; private AudioManager.OnAudioFocusChangeListener listener; @Override protected voidonResume() {
    if(audioManager! = null) { audioManager.abandonAudioFocus(listener); audioManager = null; } super.onResume(); } @Override protected voidonPause() {
    audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    listener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
        }
    };
    int result = audioManager.requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    }
    super.onPause();
}
Copy the code

The following is our personal public account (LongXuanzhigu). Our articles will be synchronized to this account, which is convenient for exchanging and learning Android knowledge and sharing personal favorite articles: