Android - dismissing Webview with html5 video

对着背影说爱祢 提交于 2019-12-08 07:56:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!