2012-09-25 69 views
1

我是Android的小菜鳥,儘管我已經閱讀了很多教程,但我仍然無法弄清楚如何在我的webview中播放youtube視頻。我可以在沒有任何問題的情況下瀏覽網站,但是當我點擊視頻時什麼也沒有發生。我曾嘗試將android:hardwareAccelerated =「true」添加到我的清單中,但沒有成功。任何幫助是極大的讚賞。以下是我迄今爲止:如何在webview中播放Youtube視頻?

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" > 

回答

0

下面是一個代碼示例:

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 getted或別的東西,你可以只是操縱字符串playVideo

希望這會有所幫助。

+0

謝謝回答。此方法適用於觀看單個視頻,但我希望能夠像通常瀏覽網站時一樣查看頻道或播放列表頁面上的視頻。這是我嘗試查看http://www.youtube.com/KitcoNews的頻道網址,該網址在設備瀏覽器中顯示得很好。你知道如何在webview中顯示嗎? –

+0

@ B.Money我不知道該怎麼做。抱歉。 –

0

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

公共類MainActivity擴展活動{

@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); 

    } 
    } 
相關問題