使用webview播放youtube视频的问题

时间:2011-06-04 16:26:19

标签: android webview youtube

我有这个代码,当在模拟器上运行时打印日志消息,这是在shouldOverrideUrlLoading方法中,当我点击视频时             06-04 08:53:24.295:VERBOSE / 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();
    }
}

}

1 个答案:

答案 0 :(得分:2)

我不知道为什么但是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)))));        }

相关问题