Dave
Dave

Reputation: 1

Playing m.youtube content in webview

Ok, so I have Java and plugins enabled. Can anyone explain why I can play many online flash videos in my webview, but nothing on m.youtube.com works?

I would be more than satisfied with a workaround that passes the video to the youtube app.

Upvotes: 0

Views: 3382

Answers (2)

bhups
bhups

Reputation: 14895

I am afraid you can not do this until native android video player start supporting this. But there is a very dirty workaround to launch youtube app.

WebView w = new WebView(this);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setBuiltInZoomControls(true);
//need to to change the user-agent to fool server
w.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
w.setOnTouchListener(new OnTouchListener(){
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    //get the URL of the touched anchor tag
    WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
    String str = hr.getExtra();
    //check if it is the URL of the thumbnail of the video
    //which looks like
    //http://i.ytimg.com/vi/<VIDEOID>/hqdefault.jpg?w=160&h=96&sigh=7exXMTRY7yiZm4hS4V_f9uVO-GU
    if(str!=null && str.startsWith("http://i.ytimg.com/vi/")){
      String videoId = str.split("\\/")[4];
      //Everything is in place, now launch the activity
      Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" +videoId));
      v.getContext().startActivity(i);
      return true;
    }
    return false;
  }
}
w.setWebViewClient(new WebViewClient(){
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
  }
);
w.loadUrl("http://m.youtube.com");

Upvotes: 0

Felix
Felix

Reputation: 89606

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("vnd.youtube:VIDEO_ID"));
startActivity(i);

Also, check this out: http://it-ride.blogspot.com/2010/04/android-youtube-intent.html

Edit: You can probably also do something amongst the lines of:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.youtube.com/watch?v=VIDEO_ID"));
startActivity(i);

Which (I think) will give the user a choice of whether to open it in the browser or the YouTube app. Useful for new devices that have flash and that may not have the YouTube app.

Upvotes: 4

Related Questions