开发者

Webview links to m.youtube.com instead of youtube app

I have my app which opens a webpage which has links to youtube videos on it.

I have 2 problems:

  1. The links open m.youtube.com fine but the videos do not play. Just highlighted in orange then nothing happens.

  2. No option to load in youtube app.

Any ideas? Im test on my galaxy S2 rather than emulator as i know the emu wont have flash or youtube app.

Many Thanks for any assistance you will be a life saver!

Here is my code:

 package com.mytest;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class mytestActivity extends Activity

    {

        final Activity activity = this;

        private WebView webview;


        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // Check if the key event was the BACK key and if there's history
            if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
                webview.goBack();
                return true;
            }
            // If it wasn't the BACK key or there's no web page history, bubble up to the default
            // system behavior (probably exit the activity)
            return super.onKeyDown(keyCode, event);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
                        setContentView(R.layout.main);

                        // Don't create another webview reference here,
                        // just use the one you declared at class level.
                        webview = (WebView) findViewById(R.id.webview);
                        webview.getSettings().setJavaScriptEnabled(true);
                        webview.getSettings().setPluginsEnabled(true);

                            webview.setWebChromeClient(new WebChromeClient() {
                            public void onProgressChanged(WebView view, int progress)
                            {
                                activity.setTitle("Loading...");
                                activity.setProgress(progress * 100);

                                if(progress == 100)
                                    activity.setTitle(R.string.app_name);
                            }
                        });

            webview.setWebViewClient(new WebViewClient() {
                @Overri开发者_如何学编程de
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                {
                    // Handle the error
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    view.loadUrl(url);
                    return true;
                }


            });


            webview.loadUrl("http://mytesturl");

    }


I have this in my WebViewClient and it opens youtube videos the native app:

 @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("youtube.com")){

            int indexStart = url.indexOf("?v=")+3;
            int indexEnd = url.indexOf("&", indexStart);
            if(indexEnd<indexStart)
                return false;
            String videoId = url.substring(indexStart,indexEnd);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://"+videoId)));

            return true;
        }
        else
        {
            return false;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜