【问题标题】:How do i play Youtube videos in webview?如何在 webview 中播放 Youtube 视频?
【发布时间】:2012-09-25 00:00:37
【问题描述】:

我是 android 的菜鸟,尽管我阅读了许多教程,但我仍然无法弄清楚如何在我的 webview 中播放 youtube 视频。我可以毫无问题地浏览该网站,但是当我点击视频时,什么也没有发生。我尝试将 android:hardwareAccelerated="true" 添加到我的清单中,但没有成功。任何帮助是极大的赞赏。这是我目前所拥有的:

  huffingtonpost = (WebView)findViewById(R.id.webView1);
    huffingtonpost.setWebViewClient(new WebViewClient());
    huffingtonpost.getSettings().setJavaScriptEnabled(true);
    huffingtonpost.getSettings().setPluginState(PluginState.ON);
    huffingtonpost.getSettings().setUseWideViewPort(true);
    huffingtonpost.getSettings().setLoadWithOverviewMode(true);

    try{
    huffingtonpost.loadUrl("http://www.youtube.com/KitcoNews");
    }catch (Exception e){
        e.printStackTrace();
    } 

我的清单

 <application
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

【问题讨论】:

    标签: android flash video webview youtube


    【解决方案1】:

    这是一个代码示例:

    myWebView = (WebView) findViewById( R.id.webview_compontent );
    
    String playVideo= "<html><body>Youtube video .. <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"
    
    myWebView.loadData(playVideo, "text/html", "utf-8");
    

    如果 URL 是从 EditText 或其他东西中获取的,您只需操作字符串 playVideo

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回答。这种方法适用于观看单个视频,但我希望能够像正常浏览网站时一样在频道或播放列表页面上观看视频。这是我试图查看youtube.com/KitcoNews 的频道网址,它在设备浏览器中显示良好。你知道如何在 webview 中显示它吗?
    • @B.Money 我不知道该怎么做。对不起。
    【解决方案2】:

    如何在 android webview 中播放 Youtube 频道?

    公共类 MainActivity 扩展 Activity {

       @Override
         public void onCreate(Bundle savedInstanceState) {
    
              super.onCreate(savedInstanceState);
    
              //setContentView(R.layout.main);
    
            String html="http://www.youtube.com/KitcoNews";
              // Let's display the progress in the activity title bar, like the
              // browser app does.
              getWindow().requestFeature(Window.FEATURE_PROGRESS);
    
              WebView webview = new WebView(this);
              setContentView(webview);
    
              webview.getSettings().setJavaScriptEnabled(true);
    
              final Activity activity = this;
              webview.setWebChromeClient(new WebChromeClient() {
              public void onProgressChanged(WebView view, int progress) {
    
                 activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
    
                    if(progress == 100)
                        activity.setTitle(""Done);
    
              }
            });
    
    
    webview.setWebViewClient(new WebViewClient() {
    
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
              //Users will be notified in case there's an error (i.e. no internet connection)
              Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }
     });
           //This will load the webpage that we want to see
            webview.loadUrl(html);
    
         }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2013-11-19
      • 2012-12-14
      • 2012-06-07
      相关资源
      最近更新 更多