Video plays only once in Webview of Android

被刻印的时光 ゝ 提交于 2019-11-30 08:39:10

问题


I am succeeded to play streamed Youtube video from HTML5 content in Webview in Android but now problem is that video plays only first time. After that VideoView only goes to end of the video file.

I tried clearing cache as suggested here but no luck.

What could be the possible solution for this problem?

Please try following code to run Video, this has been using some suggestions given on stackoverflow.com

WebView webView = (WebView) findViewById(R.id.product_details_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebChromeClient(new chromeClient());
webView.setWebViewClient(new WebViewClient(){

});
webView.loadUrl(url);

public class chromeClient extends WebChromeClient implements OnCompletionListener,  
OnErrorListener{
    private WebView wv;
    private VideoView mVideoView;
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new   
FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        if (view instanceof FrameLayout) {
            wv = (WebView)findViewById(R.id.product_details_webview);
            mCustomViewContainer = (FrameLayout) view;
            mCustomViewCallback = callback;
            mContentView = (LinearLayout)findViewById(R.id.linearlayout1);
            if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
                mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
                // frame.removeView(video);
                mContentView.setVisibility(View.GONE);
                mCustomViewContainer.setVisibility(View.VISIBLE);
                setContentView(mCustomViewContainer);
                mVideoView.setOnCompletionListener(this);
                mVideoView.setOnErrorListener(this);
                mVideoView.start();

            }
        }
    }

    public void onHideCustomView() {
        if (mVideoView == null){
            return;
        }else{
        // Hide the custom view.
        mVideoView.setVisibility(View.GONE);
        // Remove the custom view from its container.
        mCustomViewContainer.removeView(mVideoView);
        mVideoView = null;
        mCustomViewContainer.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();
        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
        }
    }


    public void onCompletion(MediaPlayer mp) {
        mp.stop();
        mCustomViewContainer.setVisibility(View.GONE);
        onHideCustomView();
        setContentView(mContentView);
    }

    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
        setContentView(mContentView);
        return true;
    }
  }

回答1:


Add in your chrome client :

        @Override public void onPrepared(MediaPlayer mp) {

        customViewCallback.onCustomViewHidden();
    }

where customViewCallback = callback;




回答2:


You got a video working inside a WebView with HTML5? Congrats! I failed trying to do that :(

To fix my issue I ended up leaving a place holder in the HTML and handling the video from native code using the MediaPlayer class.




回答3:


I had this problem as well. I had to add a seek(0). Seems like the next video played from exactly where the last video ended. So I had to go to the beginning of the video before playing.



来源:https://stackoverflow.com/questions/8310550/video-plays-only-once-in-webview-of-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!