problem playing youtube video using webview
I have this code which when runs on emulator prints the log message which is in shouldOverrideUrlLoading method when i click on the video something like 06-04 08:53:24.295: VERBOSE/url(502): vnd.youtube:aEb80IUiLog?vndapp=youtube_mobile&vndclient=mv-google&vndel=profile
but when i test this on my htc desire, the log message doesnt show up hence cannot play the video. what i am missing here. the device native android browser plays all youtube videos.
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(Int开发者_开发知识库ent.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();
}
}
}
i dont know why but webviewclient class causes the problem , we can run it in default browser remove the webviewclient class and simly load the url of youtube you will be able to run it on 2.2 for 2.1 just use intent and through parse method open it through youtube application
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length(),n))))); }
精彩评论