android - 使用 webview 播放 youtube 视频时出现问题

标签 android webview youtube

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

但是当我在我的 HTC 愿望上测试这个时,日志消息没有出现,因此无法播放视频。我在这里缺少什么。设备原生 android 浏览器播放所有 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();
    }
}

}

最佳答案

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

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

关于android - 使用 webview 播放 youtube 视频时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237925/

相关文章:

android - 如何在 Android ListView 为空时隐藏页脚?

java - 编程语言中的 "Explicitly"和 "Implicitly"有什么区别?

Swift:如何让 webview 重新加载它的 url

javascript - 安卓 WebView "Uncaught SyntaxError: Unexpected token ILLEGAL"

youtube - 如何告诉youtube我的网站是嵌入式视频的流量来源

android - 为什么在 onCreate() 之外显示 Toast 会使我的应用程序崩溃?

java - Android项目中assets-文件夹的访问方式是什么?

xamarin - 带有位置(纬度和经度)的 Xamarin.Forms (PCL) WebView

javascript - YouTube API v3对用户进行身份验证,然后获取其视频

PHP YouTube Api V3 MaxResults 不工作