【问题标题】:How do I play YouTube videos in an Android app if a user does not have the YouTube app installed?如果用户没有安装 YouTube 应用程序,我如何在 Android 应用程序中播放 YouTube 视频?
【发布时间】:2014-02-10 19:57:21
【问题描述】:

我正在尝试在 Android 应用中播放 YouTube 视频。我正在使用 YouTubePlayer API,它运行良好。但问题是我想把这个播放器放在图书馆里,如果没有安装 YouTube 应用程序,那么电影就不会显示,而且我还有另一个问题,在一些旧手机上我需要先升级 YouTube 应用程序。这是其他公司将导入的库我不想依赖于 YouTube 应用程序的版本或用户是否拥有 YouTube 应用程序。如何优雅降级以在应用内播放视频?

【问题讨论】:

    标签: android youtube youtube-api android-youtube-api


    【解决方案1】:

    使用 Android Player API 中的 YouTubeIntents class 优雅降级。下面是一些示例代码来检测用户可以做什么:

    if(YouTubeIntents.isYouTubeInstalled(context)) {
       if(YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(context) == YouTubeInitializationResult.SUCCESS) {
       // start the YouTube player
       context.startActivity(
       YouTubeStandalonePlayer.createVideoIntent((Activity) context, "developer_key", videoId));
     } else if(YouTubeIntents.canResolvePlayVideoIntent(context)) {
       // Start an intent to the YouTube app
       context.startActivity(
       YouTubeIntents.createPlayVideoIntent(context, videoId));
     }
    }
    // Falls through: last resort - render a webview with an iframe
    

    如果您好奇,我们会在our Google I/O talk from 2013 中浏览此代码。从 6:17 左右开始。

    【讨论】:

    • 不能在 YouTube 应用中打开视频,因为我们正在制作一个库,其他开发人员将包含在内。他们不会想离开他们的应用程序。
    • 在这种情况下,只需删除“else if”并使用 iframe 播放器降级为 WebView。
    • 我试试这个,但它不能在我的旧测试手机(Android 2.3.5,三星 Galaxy S)上播放视频。它只是旋转风团,10 秒后出现类似的视频。它应该适用于每部 2.3+ 的手机吗?
    • 不幸的是,网页视图中的 iframe 播放器几乎无法保证。但是 - 旧手机的百分比已显着下降。您应该关注这一点并测试此用例的原因是针对 Kindle Fire 设备,我们认为它有可能产生更大的市场影响。 Kindle Fire 设备目前不支持应用内播放所需的内容。
    • 是的,最后看起来我们会选择这个选项。它不是完美的,但它有效。感谢您的回答。
    猜你喜欢
    • 2011-06-06
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2018-08-06
    • 2014-10-28
    • 2023-03-16
    • 2011-04-25
    相关资源
    最近更新 更多