WebView and HTML5 <video>

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

    I know this thread is several months old, but I found a solution for playing the video inside the WebView without doing it fullscreen (but still in the media player...). So far, I didn't find any hint on this in the internet so maybe this is also interesting for others. I'm still struggling on some issues (i.e. placing the media player in the right section of the screen, don't know why I'm doing it wrong but it's a relatively small issue I think...).

    In the Custom ChromeClient specify LayoutParams:

    // 768x512 is the size of my video
    FrameLayout.LayoutParams LayoutParameters = 
                                         new FrameLayout.LayoutParams (768, 512); 
    

    My onShowCustomView method looks like this:

    public void onShowCustomView(final View view, final CustomViewCallback callback) {
         // super.onShowCustomView(view, callback);
         if (view instanceof FrameLayout) {
             this.mCustomViewContainer = (FrameLayout) view;
             this.mCustomViewCallback = callback;
             this.mContentView = (WebView) this.kameha.findViewById(R.id.webview);
             if (this.mCustomViewContainer.getFocusedChild() instanceof VideoView) {
                 this.mCustomVideoView = (VideoView) 
                                         this.mCustomViewContainer.getFocusedChild();
                 this.mCustomViewContainer.setVisibility(View.VISIBLE);
                 final int viewWidth = this.mContentView.getWidth();
                 final int viewLeft = (viewWidth - 1024) / 2;
                 // get the x-position for the video (I'm porting an iPad-Webapp to Xoom, 
                 // so I can use those numbers... you have to find your own of course...
                 this.LayoutParameters.leftMargin = viewLeft + 256; 
                 this.LayoutParameters.topMargin = 128;
                 // just add this view so the webview underneath will still be visible, 
                 // but apply the LayoutParameters specified above
                 this.kameha.addContentView(this.mCustomViewContainer, 
                                                 this.LayoutParameters); 
                 this.mCustomVideoView.setOnCompletionListener(this);
                 this.mCustomVideoView.setOnErrorListener(this);
                 // handle clicks on the screen (turning off the video) so you can still
                 // navigate in your WebView without having the video lying over it
                 this.mCustomVideoView.setOnFocusChangeListener(this); 
                 this.mCustomVideoView.start();
             }
         }
     }
    

    So, I hope I could help... I too had to play around with video-Encoding and saw different kinds of using the WebView with html5 video - in the end my working code was a wild mix of different code-parts I found in the internet and some things I had to figure out by myself. It really was a pain in the a*.

提交回复
热议问题