Android中经常会使用到WebView来加载H5的页面,如果H5页面中有音频或者视频的播放时,还没播放完就退出界面,这个时候会发现音频或者视频还在后台播放,这就有点一脸懵逼了,下面是解决方案:
方案一:
代码语言:javascript复制在webview所在的activity中的onPause()和onResume()方法中写上两句话。
public void onPause() {
super.onPause();
webview.onPause();
}
public void onResume() {
super.onResume();
webview.onResume();
}
方案二:
代码语言:javascript复制//添加一下代码解决问题 webView退出之后音频视频还在播放问题
private AudioManager audioManager;
private AudioManager.OnAudioFocusChangeListener listener;
@Override
protected void onResume() {
if (audioManager!= null) {
audioManager.abandonAudioFocus(listener);
audioManager = null;
}
super.onResume();
}
@Override
protected void onPause() {
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();
}