【问题标题】:Playing youtube videos from a webview从 web 视图播放 youtube 视频
【发布时间】:2012-12-14 20:51:27
【问题描述】:

是否可以从 webview 播放 youtube 视频,就像我从浏览器访问 youtube 网站并且视频开始播放时一样,它会全屏播放视频。

到目前为止,这是我的代码:

webView = (WebView) v.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient()); 
webView.setWebViewClient(new WebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebChromeClient(new WebChromeClient() {
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                final FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    // get video view

                    VideoView video = (VideoView) frame.getFocusedChild();
                    Uri mUri = null;
                    try {
                        Field mUriField = VideoView.class.getDeclaredField("mUri");
                        mUriField.setAccessible(true);
                        mUri = (Uri) mUriField.get(video);
                        Log.d(TAG, "the uri is "+mUri.toString());
                        Intent i = new Intent();
                        i.setData(mUri);
                        i.setAction(Intent.ACTION_VIEW);
                        startActivity(i);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    });

webView.loadUrl("www.youtube.com");

我只想在用户点击 web 视图内的 youtube 视频上的“播放”时启动 youtube 应用,而不是之前。

需要显示整个频道,因此用户可以选择他想播放的视频,与从浏览器应用程序访问 youtube 网站的行为相同。

编辑: 按照建议,我添加了 onShowCustomView() 方法来处理播放点击,现在我得到了一些 uri,如何在全屏 videoview 中播放视频?

【问题讨论】:

  • 表示不想播放webView中的视频?如果是,那么您可以使用overlay 视图并在该视图上设置onClick 侦听器以打开youtube 应用程序。
  • 我想知道点击了哪个视频,这样我就可以开始有意识地播放 youtube 视频了。

标签: android youtube-api


【解决方案1】:
webView = (WebView) findViewById( R.id.webview_1 );

String play= "<html><body>Youtube <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"600\" height=\"300\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"

webView.loadData(play, "text/html", "utf-8");

也可以看看..
YouTube Video not playing in WebView - Android

【讨论】:

  • 我想显示整个频道,但我仍然希望用户能够在 youtube 网站中导航。
  • 我不确定你是否可以在 webview 中导航。查看它以获取一些信息..stackoverflow.com/questions/12572549/…
  • 覆盖 onShowCustomView 有帮助。
猜你喜欢
  • 1970-01-01
  • 2012-08-09
  • 2014-05-01
  • 2014-02-12
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2017-11-07
  • 2012-05-11
相关资源
最近更新 更多