1

I have been working on an appliction, the requirement states that i need to open a Youtube link on the web view. Once clicked on the video i need to open the Youtube application to play the video.

After lots of try had not been quite successful to achieve this goal! Looking forward for sugestions.

this is what i tried.

        wb = (WebView)findViewById(R.id.web);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.setWebViewClient(new MyClient());
        wb.getSettings().setPluginsEnabled(true);
        wb.getSettings().setPluginState(PluginState.ON_DEMAND);
        wb.getSettings().setAllowFileAccess(true);
        wb.getSettings().setAppCacheEnabled(true);
        wb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wb.loadUrl("http://m.youtube.com");


private class MyClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mHandler.post(new Runnable() {
                public void run() {
                    if(!pgd.isShowing()){
                        pgd.show();
                    }
                }
            });
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i("url loaded", "url::: " + url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mHandler.post(new Runnable() {
                public void run() {
                    if(pgd !=null && pgd.isShowing()){
                        pgd.dismiss();
                    }
                }
            });
            super.onPageFinished(view, url);
        }
    }

Any pointers will be highly appreciated. Thanks.

Abhinava
  • 1,030
  • 9
  • 19
  • You can get some idea from this [post][1]. [1]: http://stackoverflow.com/questions/574195/android-youtube-app-play-video-intent – Sandy Jan 31 '12 at 07:15
  • Have you found any solutions for this problem? I am facing this too :( – Wayne May 12 '12 at 13:45

5 Answers5

2

Well if you have the URL of video and also have youtube's application installed on android device, I have better idea.

Use startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("url of youtube video")));

This will pop up user and will display option of browser and YouTube, and if YouTube is selected the video will be played in YouTube player.

Ghost
  • 3,966
  • 1
  • 25
  • 36
Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
1

I faced the same issue and got the solution by adding this line:

wb.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

Hope it works for you as well.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • Thanks, but i tried this already.. yes it works too but the web page is of the Desktop not for mobile.. hence it looks quite odd... :( – Abhinava Jan 31 '12 at 09:51
0

You need to implement the WebChromeClient and set it as the client of the webview. Then handle the video playback in the onShowCustomView callback.

Ray
  • 16,025
  • 5
  • 31
  • 51
0

If you want more control over where a clicked link loads, create your own WebViewClient that overrides the shouldOverrideUrlLoading() method. MyWebViewClient is an inner class of Activity.

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());


private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if ("www.example.com".equals(request.getUrl().getHost())) {
      // This is my website, so do not override; let my WebView load the page
      return false;
    }
    // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
    startActivity(intent);
    return true;
  }
}
Hardik
  • 17,179
  • 2
  • 35
  • 40
0

Try this in onCreate():

    WebView webview = new WebView(this); 

   String htmlString = "<html> <body> <embed src=\"link of youtube video\"; type=application/x-shockwave-flash width="+DeviceWidth+" height="+DeviceHeight+"> </embed> </body> </html>";

   webview.loadDataWithBaseURL(null,htmlString ,"text/html", "UTF-8",null);
   webview.setWebViewClient(new MyClient());
Kanika
  • 10,648
  • 18
  • 61
  • 81
  • cannot put in embed as i am suppose to show channel not only a video :( anyways thanks for sugestion.. – Abhinava Jan 31 '12 at 16:29