问题
I'm using a Webview, playing different html5 videos (internal tag and Youtube videos).
When closing the Webview I tried loading a blank page + hiding the view + pausing the webview. The Webview seems to be closed correctly and the video stops playing.
The problem is on Galaxy S4 + S2, when opening the Webview again and playing another video. The new video is been paused immediately after being played. When I press "play" again, the video is automatically paused again. This will happen with every video that I'll try to load (Youtube and internal ), until I'll kill my app. After killing my app everything will work normally again. Is it possible that the first view/video is not terminated so each new video will automatically be paused, in order to prevent the two to play at the same time?
Tnx!
Yaniv
回答1:
After struggling with this issue for few days, I found the solution. It appears that when closing the Webview, the Mediaplay probably did not have focus, which prevented him from pausing/dismissing. This prevented all other Webviews with Mediaplayers from playing (automatically paused each time trying to play, with no ability to resume).
When adding this code:
@Override
protected void onPause() {
super.onPause();
((AudioManager)getSystemService(
Context.AUDIO_SERVICE)).requestAudioFocus(
new OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {}
}, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
The problem was completely solved. The Mediaplayer was dismissed/paused (not sure which, have to check), and a new Webview with a new Mediaplayer can gain focus and play normally.
Hope it could help other people struggling with this issue..
来源:https://stackoverflow.com/questions/20480111/android-dismissing-webview-with-html5-video