【问题标题】:Force video to open in Youtube app on Android强制视频在 Android 上的 Youtube 应用中打开
【发布时间】:2012-06-22 06:45:47
【问题描述】:

我有一个链接到 youtube 视频的移动网站。在 Android 上,单击此链接会弹出一个对话框,要求用户“使用”他们的浏览器或 Youtube 应用程序“完成操作”。

有没有办法绕过这个屏幕,直接在 Youtube 应用中播放视频? (例如使用 youtube:// URL。)

谢谢!

【问题讨论】:

    标签: android android-intent youtube


    【解决方案1】:

    您可以这样做:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
    startActivity(intent);
    

    id是url中问号后面的标识符。例如:youtube.com/watch?v=ID

    另一种方式是:

    Intent videoIntent = new Intent(Intent.ACTION_VIEW);
    videoIntent.setData(url);
    videoIntent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
    startActivity(videoIntent);
    

    ......

    【讨论】:

    • 谢谢,我明天试试 vnd.youtube:// 网址看看它是否有效。
    • 有没有办法(从移动网站)检查设备是否可以打开 vnd.youtube:// 网址?我希望我的网站打开 Android 的 vnd.youtube:// URL 和常规的 youtube.com/watch? iOS 的链接。
    • 至少我不知道,抱歉
    • 你知道 youtube 频道的链接吗?
    • 就我而言,第一种方式通过 id 效果很好,但第二种方式不行。
    【解决方案2】:

    最好的办法

    try {
    
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    
    } catch (ActivityNotFoundException e) {
    
        // youtube is not installed.Will be opened in other available apps
    
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtube.com/watch?v=" + id));
        startActivity(i);
    }
    

    【讨论】:

    • 我尝试使用您的解决方案,但我得到“无法解析符号内容”。你能解释一下这是什么内容吗?
    【解决方案3】:

    尝试使用如下的 JavaScript 重定向:

    window.location = "vnd.youtube://the.youtube.video.url";
    

    更全面:

    if( /Android/i.test(navigator.userAgent ) ) {
        // If the user is using an Android device.
        setTimeout(function () { window.location = "market://details?id=com.google.android.youtube"; }, 25);
        window.location = "vnd.youtube://www.youtube.com/watch?v=yourVideoId";
    }
    

    如果 Youtube 应用程序被禁用,超时功能会将您重定向到 Play 商店中的 youtube 应用程序,让您启用该应用程序。第二个重定向将弹出并在 Android Youtube 应用上播放 youtube 视频。

    如果您在超时时间间隔内已经切换到 YouTube 应用,则不会调用超时函数,您不会切换到 Play 商店而是停留在 YouTube 应用中。

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 2016-01-11
      • 2013-08-09
      • 2014-06-20
      • 2019-01-16
      • 2013-05-06
      • 2013-02-08
      • 2016-03-23
      • 2011-08-16
      相关资源
      最近更新 更多