【问题标题】:How to call youtube app from WebView to play videos?如何从 WebView 调用 youtube 应用来播放视频?
【发布时间】:2014-01-15 23:15:52
【问题描述】:

我已经创建了一个使用 WebView 来查看我的网站的应用程序,该应用程序运行顺利并且没有任何错误,我什至将它上传到了 Google Play 商店。唯一的问题是我的网站中有 youtube 视频链接,但它们在我的应用程序中不起作用。它们出现在屏幕上,用户可以轻松地点击它们,但在点击视频加载标志后会出现并保持旋转而没有任何好处。我知道移动网络浏览器没有 Flash 播放器插件,我在 StakcOverFlow 上查看了所有类似的问题。所以我想做的是让我的应用程序从android系统调用YouTube应用程序,让它通过播放我的网站youtube视频来完成任务。我阅读了有关 Intent 方法的信息,但找不到任何有用的信息。有什么建议吗?

谢谢你svenoaks。现在我在代码中有一个小错误:

package com.parse;



import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ParserActivity extends Activity {
    private WebView myWebView;
    @SuppressLint({"setJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parser);
        myWebView =(WebView) findViewById(R.id.webview);
        myWebView.loadUrl("https://parse.com/apps/aramco-app1/push_notifications    /new");
        myWebView.setWebViewClient(new HelloWebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        myWebView.setScrollbarFadingEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setSupportZoom(true);

    }
private class HelloWebViewClient extends WebViewClient  {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    {webview.loadUrl (url);
    return true;
    {
        // YouTube video link
        if (url.startsWith("vnd.youtube:"))
        {

            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
                    (String.format("http://www.youtube.com/v/%s",         url.substring("vnd.youtube:".length())))));

            return true;
        }

        return false;
    }
    }


   @Override
        public void onReceivedError(WebView view, int errorCode, String     description,         String failingUrl)
    {
        Toast.makeText(WebActivity.this, "Oh no! " + description,             Toast.LENGTH_SHORT).show();
    }
});


@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
    {
        myWebView.goBack();
        return true;
        }
        return super.onKeyDown(keyCode, event); 
    }
public boolean setIsZoomInEnabled;

}
     class WebAppInterface {
        Context mContext;
        WebAppInterface(Context c) {
            mContext = c;
        }

        private void show()
        {
            // TODO: Implement this method
    }

}

onReceive 方法有一个错误。

【问题讨论】:

    标签: android eclipse webview youtube


    【解决方案1】:

    这在 KitKat 之前对我有用,但现在我什至无法在 webview 中看到要点击的视频,因为没有插件。希望它对你有用:

    webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                // YouTube video link
                if (url.startsWith("vnd.youtube:"))
                {
    
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
                            (String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length())))));
    
                    return true;
                }
    
                return false;
    
            }
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                Toast.makeText(WebActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });
    

    【讨论】:

    • 好吧,看起来我正在强制我的应用程序启动 Youtube 应用程序,而不是每当被点击的 ulr 以 vnd 开头时什么都不做。因此 Youtube 链接。谢谢,但是现在当我将代码添加到我的 WebViewClient 时,我最后遇到了一个问题,它看起来很像变量: public void onReceivedError(WebView view, int errorCode, String description, String failedUrl) { Toast.makeText(WebActivity.this, "哦不!" + 描述, Toast.LENGTH_SHORT).show(); } }); WebActivity 和结尾有错误
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 2012-06-07
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2011-01-18
    • 2013-07-13
    • 2014-02-10
    相关资源
    最近更新 更多