【问题标题】:How I Can hide the video option from WebView player in Android如何在 Android 中隐藏 WebView 播放器的视频选项
【发布时间】:2021-02-05 09:46:00
【问题描述】:

我试图使用 WebView 从 URL 显示视频,它运行良好,但我想隐藏用于在网络中下载视频的视频选项

enter image description here

这是我使用的代码

  web_view.setWebChromeClient(new ChromeClient(activity) {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);

            }

        });


        WebSettings webSettings = web_view.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);

        web_view.loadUrl(data.get(position).getVideo());

而我使用的 ChromeClinet 类是:

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

        Activity activity;


        ChromeClient(Activity activity) {
            super();
            this.activity = activity;
        }

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

        public void onHideCustomView() {
            ((FrameLayout) activity.getWindow().getDecorView()).removeView(this.mCustomView);
            this.mCustomView = null;
            activity.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
            activity.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 = activity.getWindow().getDecorView().getSystemUiVisibility();
            this.mOriginalOrientation = activity.getRequestedOrientation();
            this.mCustomViewCallback = paramCustomViewCallback;
            ((FrameLayout) activity.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
            activity.getWindow().getDecorView().setSystemUiVisibility(getScreenResolution() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }

        private int getScreenResolution() {
            WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;

            return height;
        }


    }

如何从网络上播放此视频,或者是否可以创建自定义播放器

【问题讨论】:

    标签: android webview


    【解决方案1】:

    简短回答: 在您的页面作为 javascript 加载到 webview 后注入 document.getElementById("video_id-in_webpage").removeAttribute("controls")

    长答案:

    您似乎正在 webView 中加载外部 url:web_view.loadUrl(data.get(position).getVideo());

    我认为这实际上与网页设计有关,而不是 android。 如果您是网站开发人员,或者您可以在 web 端(不是 android)向他们询问以下解决方案,可能会解决您的问题。

    HTML5 video - show/hide controls programmatically

    HTML5 Video // Completely Hide Controls

    但是,如果您想在 android 端执行此操作,则可以在页面完全加载后向 Web 视图注入一些 CSS 或 javascript 代码,以隐藏 html video 标记上的控制器。您可以在上面的链接中找到要注入的代码。 怎么注射?!这个问题在stackoverflow中也得到了回答 Inject javascript file to my site with webview in androidinject CSS to a site with webview in android

    public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
        // here you can detect if page has loaded or not to inject javascript 
    view.loadUrl("javascript: document.getElementById('video_id_in_webpage').removeAttribute('controls');"
                }
    

    【讨论】:

    • 用户点击视频选项时如何设置监听器?
    • 或者,如果您从 URL 知道好的视频播放器可以在 android 中使用回收站视图
    • @Mohammad Sallout 用于创建侦听器,您可以在 android 中创建一个 javascript 接口并连接它们。如果您有视频的 url,我认为 VideoView 比 WebView 更好。
    猜你喜欢
    • 2014-10-26
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2013-12-17
    • 2013-07-20
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多