Opening an activity with a webview containing a HTML5 video for a second time

血红的双手。 提交于 2020-01-06 04:02:05

问题


I am serving video's in a webview in Android using Appcelerator Titanium. On iOS this works like a charm. But on Android this gives some problems. It goes wrong the second time an activity is opened with a webview that holds a html5 video element.

Steps to reproduce:

(Also see this youtube video: http://www.youtube.com/watch?v=0MpSpfJNyOk )

The first time opening a video/activity with html5 video it shows the video and I am able to play it. When I close the window/activity and open a new window with another video the webview loads and the html5 video play loads as well. But playing it is not possible. When pressing the fullscreen button and pressing the play/pause button twice the video begins to play in fullscreen.

When pausing the video before closing the activity/window the video in the next window is able to play.

The youtube video shows the behavior on a Samsung Galaxy Tab 3 10.1" running Android 4.2 using this test project https://dl.dropboxusercontent.com/u/1849335/WebviewTest.zip

You can understand it took me a while to understand this pattern :)

Context:

I am using the 3.2.GA Titanium SDK with this fix (https://github.com/appcelerator/titanium_mobile/pull/5150) merged. In that change they fixed that the onPause() method should always be called on the webview when closing a activity otherwise the plugin processes will continue to play.

The files that are responsible for this behavior are: https://github.com/wienke/titanium_mobile/tree/master/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview https://github.com/wienke/titanium_mobile/blob/master/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java

Curious if people also see this behavior without Titanium.


回答1:


The solution was eventually provided in this question:

Android (Samsung S4) HTML5 video pauses

Adding this:

pause();
((AudioManager)activity.getSystemService(
                activity.AUDIO_SERVICE)).requestAudioFocus(
                        new OnAudioFocusChangeListener() {
                            @Override
                            public void onAudioFocusChange(int focusChange) {}
                        }, AudioManager.STREAM_MUSIC, 
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

to the SDK onPause method here:

https://github.com/wienke/titanium_mobile/blob/master/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java#L427

fixed the problem.

I filed a defect to Appcelerator. Hope they include this soon in their SDK.



来源:https://stackoverflow.com/questions/20814894/opening-an-activity-with-a-webview-containing-a-html5-video-for-a-second-time

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