WebView and HTML5 <video>

后端 未结 13 1902
面向向阳花
面向向阳花 2020-11-22 05:59

I\'m piecing together a cheapo app that amongst other things \"frames\" some of our websites... Pretty simple with the WebViewClient. until I hit the video.

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:14

    I answer this topic just in case someone read it and is interested on the result. It is possible to view a video element (video html5 tag) within a WebView, but I must say I had to deal with it for few days. These are the steps I had to follow so far:

    -Find a properly encoded video

    -When initializing the WebView, set the JavaScript, Plug-ins the WebViewClient and the WebChromeClient.

    url = new String("http://broken-links.com/tests/video/"); 
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setWebChromeClient(chromeClient);
    mWebView.setWebViewClient(wvClient);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginState(PluginState.ON);
    mWebView.loadUrl(url);
    

    -Handle the onShowCustomView in the WebChromeClient object.

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        if (view instanceof FrameLayout){
            FrameLayout frame = (FrameLayout) view;
            if (frame.getFocusedChild() instanceof VideoView){
                VideoView video = (VideoView) frame.getFocusedChild();
                frame.removeView(video);
                a.setContentView(video);
                video.setOnCompletionListener(this);
                video.setOnErrorListener(this);
                video.start();
            }
        }
    }
    

    -Handle the onCompletion and the onError events for the video, in order to get back to the web view.

    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "Video completo");
        a.setContentView(R.layout.main);
        WebView wb = (WebView) a.findViewById(R.id.webview);
        a.initWebView();
    }
    

    But now I should say there are still an important issue. I can play it only once. The second time I click on the video dispatcher (either the poster or some play button), it does nothing.

    I would also like the video to play inside the WebView frame, instead of opening the Media Player window, but this is for me a secondary issue.

    I hope it helps somebody, and I would also thank any comment or suggestion.

    Saludos, terrícolas.

提交回复
热议问题