Playing HTML5 video on fullscreen in android webview

后端 未结 8 2101
迷失自我
迷失自我 2020-11-22 10:23

Well, I\'ve been searching few days already, how to display HTML5 video in full-screen mode on android WebView.

I managed to play HTML5 videos on my webview. Problem

8条回答
  •  渐次进展
    2020-11-22 10:40

    Edit: please see my other answer, as you probably don't need this now.

    As you said, in API levels 11+ a HTML5VideoFullScreen$VideoSurfaceView is passed. But I don't think you are right when you say that "it doens't have a MediaPlayer".

    This is the way to reach the MediaPlayer instance from the HTML5VideoFullScreen$VideoSurfaceView instance using reflection:

    @SuppressWarnings("rawtypes")
    Class c1 = Class.forName("android.webkit.HTML5VideoFullScreen$VideoSurfaceView");
    Field f1 = c1.getDeclaredField("this$0");
    f1.setAccessible(true);
    
    @SuppressWarnings("rawtypes")
    Class c2 = f1.getType().getSuperclass();
    Field f2 = c2.getDeclaredField("mPlayer");
    f2.setAccessible(true);
    
    Object ___html5VideoViewInstance = f1.get(focusedChild); // Look at the code in my other answer to this same question to see whats focusedChild
    
    Object ___mpInstance = f2.get(___html5VideoViewInstance); // This is the MediaPlayer instance.
    

    So, now you could set the onCompletion listener of the MediaPlayer instance like this:

    OnCompletionListener ocl = new OnCompletionListener()
    {
        @Override
        public void onCompletion(MediaPlayer mp)
        {
            // Do stuff
        }
    };
    
    Method m1 = f2.getType().getMethod("setOnCompletionListener", new Class[] { Class.forName("android.media.MediaPlayer$OnCompletionListener") });
    m1.invoke(___mpInstance, ocl);
    

    The code doesn't fail but I'm not completely sure if that onCompletion listener will really be called or if it could be useful to your situation. But just in case someone would like to try it.

提交回复
热议问题