我有这段代码,当我在模拟器上运行时,当我点击视频时打印出 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/