【问题标题】:problem playing youtube video using webview使用 webview 播放 youtube 视频时出现问题
【发布时间】:2011-06-04 16:26:18
【问题描述】:

我有这段代码,当我在模拟器上运行时,当我点击视频时会打印出 shouldOverrideUrlLoading 方法中的日志消息 06-04 08:53:24.295: 详细/url(502): vnd.youtube:aEb80IUiLog?vndapp=youtube_mobile&vndclient=mv-google&vndel=profile

但是当我根据我的 htc 需求进行测试时,日志消息没有显示,因此无法播放视频。我在这里缺少什么。设备原生安卓浏览器播放所有 youtube 视频。

     import android.app.Activity;
     import android.content.ComponentName;
     import android.content.Intent;
     import android.net.Uri;
     import android.os.Bundle;
     import android.util.Log;
     import android.webkit.WebView;
     import android.webkit.WebViewClient;




    public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{

    WebView web=(WebView) findViewById(R.id.webkitWebView1);



//  web.getSettings().setBuiltInZoomControls(true);

    web.getSettings().setJavaScriptEnabled (true);
   //        web.getSettings().setJavaScriptCanOpenWindowsAutomatically (false);
   //        web.getSettings().setPluginsEnabled (true);
   //        web.getSettings().setSupportMultipleWindows (false);
   //        web.getSettings().setSupportZoom (false);
   //        web.setVerticalScrollBarEnabled (false);
   //        web.setHorizontalScrollBarEnabled (false);
   //        web.getSettings(). setAppCacheEnabled(true);
   //        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    web.loadUrl("http://www.youtube.com/cg225");


    web.setWebViewClient(new WebViewClient()
    {
         @ Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)
     {

         Log.v("url",url);
      // YouTube video link
      if (url.startsWith("vnd.youtube:"))
      {
       int n = url.indexOf("?");
       if (n > 0)
       {
        startActivity(new Intent(Intent.ACTION_VIEW,                 Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length(),n)))));
       }
       return (true);
      }

      return (false);
     }
    });

}catch(Exception e){
    e.printStackTrace();
    }
}

}

【问题讨论】:

    标签: android webview youtube


    【解决方案1】:

    我不知道为什么,但是 webviewclient 类会导致问题,我们可以在默认浏览器中运行它 删除 webviewclient 类并简单地加载 youtube 的 url 你将能够在 2.2 上运行它 2.1 只需使用意图并通过 parse 方法通过 youtube 应用程序打开它

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length( ),n))))); }

    【讨论】:

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