0

我有我的应用程序,它打开一个网页,上面有 youtube 视频的链接。

我有两个问题:

  1. 链接可以正常打开 m.youtube.com,但无法播放视频。只是以橙色突出显示然后没有任何反应。

  2. 没有选项可以在 youtube 应用程序中加载。

有任何想法吗?我在我的 Galaxy S2 而不是模拟器上进行测试,因为我知道 Emu 不会有 Flash 或 youtube 应用程序。

非常感谢您的帮助,您将成为救生员!

这是我的代码:

 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() {
                @Override
                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");

    }
4

1 回答 1

1

我在我的 WebViewClient 中有这个,它会打开原生应用程序的 youtube 视频:

 @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;
        }
    }
于 2012-01-18T19:28:12.537 回答