WebView and HTML5 <video>

后端 未结 13 1689
面向向阳花
面向向阳花 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:09

    mdelolmo's answer was incredibly helpful, but like he said, the video only plays once and then you can't open it again.

    I looked into this a bit and here is what I found, in case any weary WebView travelers like myself stumble on this post in the future.

    First off, I looked at the VideoView and MediaPlayer's documentation and got a better sense of how those work. I strongly recommend those.

    Then, I looked at the source code to see how the Android Browser does it. Do a page find and go look at how they handle onShowCustomView(). They keep a reference to the CustomViewCallbackand to the custom view.

    With all of that, and with mdelolmo's answer in mind, when you are done with the video, all you need to do is two things. First, on the VideoView that you saved a reference to, call stopPlayback() that will release the MediaPlayer to be used later elsewhere. You can see it in the VideoView source code. Second, on the CustomViewCallback you saved a reference to call CustomViewCallback.onCustomViewHidden().

    After doing those two things, you can click on the same video or any other video and it will open like before. No need to restart the entire WebView.

    Hope that helps.

    0 讨论(0)
  • 2020-11-22 06:10

    Well, apparently this is just not possible without using a JNI to register a plugin to get the video event. (Personally, I am avoiding JNI's since I really don't want to deal with a mess when Atom-based android tablets come out in the next few months, losing the portability of Java.)

    The only real alternative seems to be to create a new web page just for WebView and do video the old-school way with an A HREF link as cited in the Codelark url above.

    Icky.

    0 讨论(0)
  • 2020-11-22 06:13

    Actually, it seems sufficient to merely attach a stock WebChromeClient to the client view, ala

    mWebView.setWebChromeClient(new WebChromeClient());
    

    and you need to have hardware acceleration turned on!

    At least, if you don't need to play a full-screen video, there's no need to pull the VideoView out of the WebView and push it into the Activity's view. It will play in the video element's allotted rect.

    Any ideas how to intercept the expand video button?

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 06:14

    This approach works well very till 2.3 And by adding hardwareaccelerated=true it even works from 3.0 to ICS One problem i am facing currently is upon second launch of media player application is getting crashed because i have not stopped playback and released Media player. As VideoSurfaceView object, which we get in onShowCustomView function from 3.0 OS, are specific to browser and not a VideoView object as in, till 2.3 OS How can i access it and stopPlayback and release resources?

    0 讨论(0)
  • 2020-11-22 06:15

    I used html5webview to solve this problem.Download and put it into your project then you can code just like this.

    private HTML5WebView mWebView;
    String url = "SOMEURL";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWebView = new HTML5WebView(this);
        if (savedInstanceState != null) {
                mWebView.restoreState(savedInstanceState);
        } else {
                mWebView.loadUrl(url);
        }
        setContentView(mWebView.getLayout());
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
    }
    

    To make the video rotatable, put android:configChanges="orientation" code into your Activity for example (Androidmanifest.xml)

    <activity android:name=".ui.HTML5Activity" android:configChanges="orientation"/>
    

    and override the onConfigurationChanged method.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
    }
    
    0 讨论(0)
提交回复
热议问题