Android - WebView全屏视频无法在viewpager中运行

时间:2017-07-05 06:29:30

标签: java android video webview

Webview全屏视频无法在viewpager(片段)中使用:

在webview chromeclient中实现了全屏视图,但是如果单击全屏按钮,它会显示空白屏幕但播放音频。更改后,将显示视频。

WebViewFragment.java

 mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if(isAdded() && getActivity()!=null) {
                DiapromaActivity activity = (DiapromaActivity) getActivity();
                activity.showCustomView(view, callback);
            }
        }

        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
            if(isAdded() && getActivity()!=null) {
                DiapromaActivity activity = (DiapromaActivity) getActivity();
                activity.hideCustomView();
            }
        }
    });

DiapromaActivity.java

 public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
    //If there's already a custom view, this is a duplicate call, and we should
    // terminate the new view, then bail out.
    if (mCustomView != null) {
        callback.onCustomViewHidden();
        return;
    }

    //Create a reusable set of FrameLayout.LayoutParams
    FrameLayout.LayoutParams fullscreenParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT);

    //Save the drawer view into an instance variable, then hide it.
    mContentView = findViewById(R.id.root_view);
    mContentView.setVisibility(View.GONE);

    //Create a new custom view container
    mCustomViewContainer = new FrameLayout(this);
    mCustomViewContainer.setLayoutParams(fullscreenParams);
    mCustomViewContainer.setBackgroundResource(android.R.color.black);

    //Set view to instance variable, then add to container.
    mCustomView = view;
    view.setLayoutParams(fullscreenParams);
    mCustomViewContainer.addView(mCustomView);
    mCustomViewContainer.setVisibility(View.VISIBLE);

    //Save the callback an instance variable.
    mCustomViewCallback = callback;

    //Hide the action bar
    getSupportActionBar().hide();

    isFullScreen=true;
    //Set the custom view container as the activity's content view.
    setContentView(mCustomViewContainer);


}

/**
 * Method to mirror onShowCustomView from the WebChrome client, allowing WebViews in a Fragment
 * to hide custom views.
 */
public void hideCustomView() {
    if (mCustomView == null) {
        //Nothing to hide - return.
        return;
    } else {
        // Hide the custom view.
        mCustomView.setVisibility(View.GONE);

        // Remove the custom view from its container.
        mCustomViewContainer.removeView(mCustomView);
        mCustomViewContainer.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();
        mCustomView = null;

        // Show the ActionBar
        getSupportActionBar().show();

        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
        setContentView(mContentView);
        isFullScreen=false;
    }

}

0 个答案:

没有答案