最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Android (Samsung S4) HTML5 video pauses - Stack Overflow

programmeradmin3浏览0评论

I am using Android Webview to play html5 videos, including Youtube videos (my own tags and Youtube embedded iFrames). I encountered an issue with Samsung Galaxy S4, on the following scenario:

  1. Play a video.
  2. Press 'back' while/after the video is playing (view closes)
  3. Open the view again and press 'play'.
  4. The video starts to play and immediately pauses. From this moment the same will happen for each video that I'll try to play.

I tried this on a test environment with two kind of Webviews: "regular" Android Webview and HTML5 webview

The only "suspicious" thing I see is an error while pressing the 'back' (Android error):

sendUserActionEvent() mView == null

Is it a Galaxy S4 issue? An Android issue? Any idea?

tnx, Yaniv

I am using Android Webview to play html5 videos, including Youtube videos (my own tags and Youtube embedded iFrames). I encountered an issue with Samsung Galaxy S4, on the following scenario:

  1. Play a video.
  2. Press 'back' while/after the video is playing (view closes)
  3. Open the view again and press 'play'.
  4. The video starts to play and immediately pauses. From this moment the same will happen for each video that I'll try to play.

I tried this on a test environment with two kind of Webviews: "regular" Android Webview and HTML5 webview

The only "suspicious" thing I see is an error while pressing the 'back' (Android error):

sendUserActionEvent() mView == null

Is it a Galaxy S4 issue? An Android issue? Any idea?

tnx, Yaniv

Share Improve this question asked Dec 4, 2013 at 15:35 Yaniv EfraimYaniv Efraim 6,7138 gold badges54 silver badges98 bronze badges 1
  • 1 I also got this behavior see stackoverflow./questions/20814894/… and this video youtube./watch?v=0MpSpfJNyOk . Still no result. Did you have any luck in finding a solution? – Wienke Giezeman Commented Dec 29, 2013 at 11:22
Add a ment  | 

2 Answers 2

Reset to default 15 +50

It appears that if the AudioService does not have focus when you close the Webview, it is not paused. This causes several issues (another player cannot start playing etc.)

The only solution that worked for me is:

@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);
}

This will cause the current player to be paused/killed (not sure which) when going down and solve all of the experienced issues/problems.

The previous answer isn't worked to me at Android Kitkat, the following answer will work properly on all platforms.

try {
        Class.forName("android.webkit.WebView")
                .getMethod("onPause", (Class[]) null)
                .invoke(webView, (Object[]) null);
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
    } catch (NoSuchMethodException nsme) {
        nsme.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (NullPointerException nullPointerException) {
        nullPointerException.printStackTrace();
    }
发布评论

评论列表(0)

  1. 暂无评论