Javascript 在WebView中加载YouTube视频时,全屏选项不可用

Javascript 在WebView中加载YouTube视频时,全屏选项不可用,javascript,android,webview,youtube,android-webview,Javascript,Android,Webview,Youtube,Android Webview,我已经在Android应用程序中使用webview很多次了。但这一次,我在将YouTube视频加载到WebView时遇到了一个奇怪的问题 看,这是Chrome浏览器中加载的YouTube视频的屏幕截图,其中有全屏选项 现在,下面是我在webview中加载相同视频的应用程序的屏幕截图。但它没有全屏选项 您可以在两个图像中看到更改。两个屏幕截图都是从同一台设备上拍摄的。但看起来还是不一样 我的webView加载代码是 更新 我也看到了同样的问题。但不知道是否有解决方案可用。如果我理解正确,您有一

我已经在Android应用程序中使用webview很多次了。但这一次,我在将YouTube视频加载到WebView时遇到了一个奇怪的问题

看,这是Chrome浏览器中加载的YouTube视频的屏幕截图,其中有全屏选项

现在,下面是我在webview中加载相同视频的应用程序的屏幕截图。但它没有全屏选项

您可以在两个图像中看到更改。两个屏幕截图都是从同一台设备上拍摄的。但看起来还是不一样

我的webView加载代码是

更新
我也看到了同样的问题。但不知道是否有解决方案可用。

如果我理解正确,您有一个包含第二个iframe(youtube)的iframe。 尝试将allowfullscreen属性添加到“父”iframe

要获得完整的浏览器支持,它应该如下所示:

<iframe src="your_page_url" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"> </iframe>

iFrame
是一个选项,但您可以尝试此选项

Android的WebView和WebChromeClient类扩展支持 全面工作的HTML5视频支持

我还没有尝试过,但希望能对您有所帮助。

在java文件中进行以下更改:
并在java文件中添加2个类,即类浏览器和类MyWebClient

class Browser
        extends WebViewClient
{
    Browser() {}

    public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString)
    {
        paramWebView.loadUrl(paramString);
        return true;
    }
}

public class MyWebClient
        extends WebChromeClient
{
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    public MyWebClient() {}

    public Bitmap getDefaultVideoPoster()
    {
        if (MainActivity.this == null) {
            return null;
        }
        return BitmapFactory.decodeResource(MainActivity.this.getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)MainActivity.this.getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {
        if (this.mCustomView != null)
        {
            onHideCustomView();
            return;
        }
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = MainActivity.this.getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = MainActivity.this.getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)MainActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(3846);
    }
}

关于科特林的
@Ameya Bonkinpelliwar
的答复

class WebChrome(activity: Activity) : WebChromeClient() {

    private val activityRef = WeakReference(activity)

    private var customView: View? = null
    private var customViewCallback: CustomViewCallback? = null

    private var originalOrientation = 0
    private var originalSystemUiVisibility = 0

    override fun onProgressChanged(view: WebView, progress: Int) {
        view.context.activityCallback<MainActivity> {
            onProgress(progress)
        }
    }

    override fun getDefaultVideoPoster(): Bitmap? {
        return activityRef.get()?.run {
            BitmapFactory.decodeResource(applicationContext.resources, 2130837573)
        }
    }

    override fun onHideCustomView() {
        activityRef.get()?.run {
            (window.decorView as ViewGroup).removeView(customView)
            customView = null
            window.decorView.systemUiVisibility = originalSystemUiVisibility
            requestedOrientation = originalOrientation
        }
        customViewCallback?.onCustomViewHidden()
        customViewCallback = null
    }

    override fun onShowCustomView(view: View?, viewCallback: CustomViewCallback?) {
        if (customView != null) {
            onHideCustomView()
            return
        }
        customView = view
        activityRef.get()?.run {
            originalSystemUiVisibility = window.decorView.systemUiVisibility
            originalOrientation = requestedOrientation
            customViewCallback = viewCallback
            (window.decorView as ViewGroup).addView(
                customView,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            window.decorView.systemUiVisibility = 3846
        }
    }
}
类WebChrome(活动:活动):WebChromeClient(){
private val activityRef=WeakReference(活动)
私有变量customView:视图?=null
私有变量customViewCallback:customViewCallback?=null
private var originalOrientation=0
private var originalsystemivilibility=0
覆盖已更改的进度(视图:WebView,进度:Int){
view.context.activityCallback{
进步(进步)
}
}
覆盖有趣的getDefaultVideoPoster():位图{
返回activityRef.get()?。运行{
BitmapFactory.decodeResource(applicationContext.resources,2130837573)
}
}
覆盖onHideCustomView()的乐趣{
activityRef.get()?.run{
(window.decorView作为视图组)。移除视图(自定义视图)
customView=null
window.decorView.systemui可见性=原始系统兼容性
请求定向=原始定向
}
customViewCallback?.onCustomViewHidden()
customViewCallback=null
}
覆盖ShowCustomView(视图:view?,viewCallback:CustomViewCallback?){
if(customView!=null){
onHideCustomView()
返回
}
自定义视图=视图
activityRef.get()?.run{
originalsystemivilibility=window.decorView.systemui可见性
originalOrientation=请求的方向
customViewCallback=viewCallback
(window.decorView作为视图组)。添加视图(
customView,
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_父级,
ViewGroup.LayoutParams.MATCH_父级
)
)
window.decorView.systemui可见性=3846
}
}
}

我正在将url直接加载到webview。我没有在其中附加任何iframe标记。如果我附加它,它会工作吗?是的。请尝试,或者您可以做一件事。将此参数附加到youtube url
?allowfullscreen=true
。我已经尝试了您的方法。我加载视频这样的方式,但它不是加载视频。mWebView.loadUrl(“”);我在logcat中遇到如下错误:拒绝加载无效URL:%3Ciframe%20src%3D%27https://www.youtube.com/watch?v=zxyauCOc5gs“allowfullscreen='allowfullscreen'mozallowfullscreen='mozallowfullscreen'msallowfullscreen='msallowfullscreen'oallowfullscreen='oallowfullscreen'webkitallowfullscreen='webkitallowfullscreen'>03想知道为什么会这样吗答案并没有得到任何支持,但这确实有效。只有一个bug“actionBar”向下移动。仍在寻找解决方案。谢谢你的回答btw@WideFide你找到了actionBar的解决方案吗?我有同样的问题,但工作的解决方案。那些id真的必须是noname吗?@WideFide你能告诉我在哪里需要添加以上两个类吗?它是分开归档的吗?如果是,那么主要活动是课堂?为什么这个答案不正确。这对我来说很好。真的让我很开心…我在过去10天里一直在寻找类似的东西,谢谢@M DWelcome到stackoverflow。除了您提供的答案,请考虑提供一个简要解释为什么以及如何修复这个问题。
//Add WebChromeClient to your webview 
//With navigation option and player controls overlapping handlled.

    class UriChromeClient extends WebChromeClient {
            private View mCustomView;
            private WebChromeClient.CustomViewCallback mCustomViewCallback;
            protected FrameLayout mFullscreenContainer;
            private int mOriginalOrientation;
            private int mOriginalSystemUiVisibility;

            @SuppressLint("SetJavaScriptEnabled")
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog,
                                          boolean isUserGesture, Message resultMsg) {
                mWebviewPop = new WebView(getApplicationContext());
                mWebviewPop.setVerticalScrollBarEnabled(false);
                mWebviewPop.setHorizontalScrollBarEnabled(false);
                mWebviewPop.setWebViewClient(new MyWebViewClient());
                mWebviewPop.getSettings().setSupportMultipleWindows(true);
                mWebviewPop.getSettings().setJavaScriptEnabled(true);
                mWebviewPop.getSettings().setUserAgentString(mWebviewPop.getSettings().getUserAgentString().replace("; wv", ""));
    //            mWebviewPop.getSettings().setUserAgentString(USER_AGENT);
                mWebviewPop.getSettings().setSaveFormData(true);
                mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                mContainer.addView(mWebviewPop);
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(mWebviewPop);
                resultMsg.sendToTarget();

                return true;
            }

            @Override
            public void onCloseWindow(WebView window) {
                Log.d("onCloseWindow", "called");
            }

            //
            public Bitmap getDefaultVideoPoster() {
                if (mCustomView == null) {
                    return null;
                }
                return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
            }

            public void onHideCustomView() {
                ((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
                this.mCustomView = null;
                getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
                setRequestedOrientation(this.mOriginalOrientation);
                this.mCustomViewCallback.onCustomViewHidden();
                this.mCustomViewCallback = null;
            }

            public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
                if (this.mCustomView != null) {
                    onHideCustomView();
                    return;
                }
                this.mCustomView = paramView;
                this.mCustomView.setBackgroundColor(Color.BLACK);
                this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
                this.mOriginalOrientation = getRequestedOrientation();
                this.mCustomViewCallback = paramCustomViewCallback;
                ((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
                getWindow().getDecorView().setSystemUiVisibility(3846);

                this.mCustomView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                            updateControls(getNavigationBarHeight());
                        } else {
                            updateControls(0);
                        }

                    }
                });

            }

            void updateControls(int bottomMargin) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) this.mCustomView.getLayoutParams();
                params.bottomMargin = bottomMargin;
                this.mCustomView.setLayoutParams(params);
            }
        }

        int getNavigationBarHeight() {
            Resources resources = getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                return resources.getDimensionPixelSize(resourceId);
            }
            return 0;
        }


        private void loadURL(WebView view, String url) {
            ConnectivityManager cm = (ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                view.setVisibility(View.VISIBLE);
                noNetworkText.setVisibility(View.GONE);
                view.loadUrl(url);

            } else {
                Log.d(TAG, "loadURL: no network");
                view.setVisibility(View.INVISIBLE);
                noNetworkText.setVisibility(View.VISIBLE);
            }
        }

    }
class WebChrome(activity: Activity) : WebChromeClient() {

    private val activityRef = WeakReference(activity)

    private var customView: View? = null
    private var customViewCallback: CustomViewCallback? = null

    private var originalOrientation = 0
    private var originalSystemUiVisibility = 0

    override fun onProgressChanged(view: WebView, progress: Int) {
        view.context.activityCallback<MainActivity> {
            onProgress(progress)
        }
    }

    override fun getDefaultVideoPoster(): Bitmap? {
        return activityRef.get()?.run {
            BitmapFactory.decodeResource(applicationContext.resources, 2130837573)
        }
    }

    override fun onHideCustomView() {
        activityRef.get()?.run {
            (window.decorView as ViewGroup).removeView(customView)
            customView = null
            window.decorView.systemUiVisibility = originalSystemUiVisibility
            requestedOrientation = originalOrientation
        }
        customViewCallback?.onCustomViewHidden()
        customViewCallback = null
    }

    override fun onShowCustomView(view: View?, viewCallback: CustomViewCallback?) {
        if (customView != null) {
            onHideCustomView()
            return
        }
        customView = view
        activityRef.get()?.run {
            originalSystemUiVisibility = window.decorView.systemUiVisibility
            originalOrientation = requestedOrientation
            customViewCallback = viewCallback
            (window.decorView as ViewGroup).addView(
                customView,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
            window.decorView.systemUiVisibility = 3846
        }
    }
}