【问题标题】:How can we play YouTube embeded code in an Android application using webview?我们如何使用 webview 在 Android 应用程序中播放 YouTube 嵌入代码?
【发布时间】:2011-08-14 15:20:13
【问题描述】:

我们如何使用 webview 在 Android 应用程序中播放 YouTube 嵌入代码?

【问题讨论】:

    标签: android video youtube embed


    【解决方案1】:

    查看以下 SO 帖子: How to embed a YouTube clip in a WebView on Android

    试试下面的代码可能对你有帮助。我只是把它框起来供你参考

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

    Webview的使用方法请参考以下链接:http://developer.android.com/reference/android/webkit/WebView.html

    【讨论】:

    • thanx .i 与此链接具有相同的代码 ..
    • thanx vinayak。我得到黑屏,但我的 Galaxy Tab 中还没有播放视频。
    • 谢谢它的工作,但我仍然有全屏模式的问题
    • 如何在android的浏览器中启用java脚本?你能帮帮我吗?
    • 你可以使用 webVIEW.getSettings().setJavaScriptEnabled(true);在 loadData 之前,希望还为时不晚:)
    【解决方案2】:

    您好,如果您遇到黑屏,请确保您启用插件

    WebView engine = (WebView) findViewById(R.id.webview);
    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setPluginsEnabled(true);
    engine.loadUrl("http://www.youtube.com/embed/bIPcobKMB94?autoplay=1");
    

    【讨论】:

    • 这会在 2.2 中的外部网络浏览器中打开 URL。
    • @Oh Danny Boy 实现你自己的 WebViewClient 并覆盖 public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url);返回真; }
    【解决方案3】:

    在 XML 文件中添加 WebView

    <?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
    

    在 Java 文件中

    String frameVideo = "<html><body>Youtube video .. <br> <iframe width="320" height="315" src="https://www.youtube.com/embed/lY2H2ZP56K4" frameborder="0" allowfullscreen></iframe></body></html>";
    
    WebView displayVideo = (WebView)findViewById(R.id.webView);
    
    displayVideo.loadData(frameVideo, "text/html", "utf-8");
    

    教程:

    1. How to embed youtube video in android

    2. Android WebVew tutorial with example

    【讨论】:

      【解决方案4】:
      public class webview_youtube extends Activity{
      
          String video_id = "";
          ImageView back_btn;
          ProgressBar progressBar;
          html = "";
          WebView webView;
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              setContentView(R.layout.video_screen);
      
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      
      
      
                  video_id = "KK9bwTlAvgo";
      
      
              initializeViews();
          }
      
          public void initializeViews() {
              try {
      
                  webView = (WebView) findViewById(R.id.webView);
                  progressBar = (ProgressBar) findViewById(R.id.progressBar);
      
                  if (video_id.equalsIgnoreCase("")) {
                      finish();
                      return;
                  }
      
                  WebSettings ws = webView.getSettings();
                  ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                  ws.setPluginState(WebSettings.PluginState.ON);
                  ws.setJavaScriptEnabled(true);
                  webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                  webView.reload();
      
      
                      html = getHTML(video_id);
      
                  webView.loadData(html, "text/html", "UTF-8");
      
                  WebClientClass webViewClient = new WebClientClass(progressBar);
                  webView.setWebViewClient(webViewClient);
                  WebChromeClient webChromeClient = new WebChromeClient();
                  webView.setWebChromeClient(webChromeClient);
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          @Override
          protected void onDestroy() {
              super.onDestroy();
              try {
                  webView.loadData("", "text/html", "UTF-8");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          @Override
          public void onBackPressed() {
              super.onBackPressed();
              try {
                  webView.loadData("", "text/html", "UTF-8");
                  finish();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
      
          public class WebClientClass extends WebViewClient {
              ProgressBar ProgressBar = null;
      
              WebClientClass(ProgressBar progressBar) {
                  ProgressBar = progressBar;
              }
      
              @Override
              public void onPageStarted(WebView view, String url, Bitmap favicon) {
                  super.onPageStarted(view, url, favicon);
                  ProgressBar.setVisibility(View.VISIBLE);
              }
      
              @Override
              public void onPageFinished(WebView view, String url) {
                  super.onPageFinished(view, url);
                  ProgressBar.setVisibility(View.GONE);
              }
      
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  Log.i("webview-click :", "" + url.toString());
      
                  //her the youtube url gets loaded to show video
                  view.loadUrl(getHTML(video_id));
                  return true;
              }
          }
      
          public String getHTML(String videoId) {
              String html = "<iframe class=\"youtube-player\" " + "style=\"border: 0; width: 100%; height: 96%;"
                      + "padding:0px; margin:0px\" " + "id=\"ytplayer\" type=\"text/html\" "
                      + "src=\"http://www.youtube.com/embed/" + videoId
                      + "?&theme=dark&autohide=2&modestbranding=1&showinfo=0&autoplay=1\fs=0\" frameborder=\"0\" "
                      + "allowfullscreen autobuffer " + "controls onclick=\"this.play()\">\n" + "</iframe>\n";
              Log.i("video-id from html url= ", "" + html);
              return html;
          }
      
      }
      

      【讨论】:

      • 这很好用!请将 http 改为 https 以使其正常工作
      猜你喜欢
      • 1970-01-01
      • 2013-04-08
      • 2023-04-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多