【问题标题】:Webview links to m.youtube.com instead of youtube appWebview 链接到 m.youtube.com 而不是 youtube 应用
【发布时间】:2011-11-19 07:56:51
【问题描述】:

我的应用程序会打开一个网页,该网页上有指向 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");

    }

【问题讨论】:

    标签: android eclipse video youtube playing


    【解决方案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-02-22
      • 2021-07-24
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2020-04-25
      相关资源
      最近更新 更多