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,020
  • 8
  • 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

4 Answers4

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
  • 24
  • 35
Hardik Trivedi
  • 5,935
  • 4
  • 29
  • 53
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,785
  • 9
  • 55
  • 64
  • 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
  • 15,575
  • 5
  • 27
  • 50
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,558
  • 17
  • 59
  • 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