我正在尝试在 Android 应用程序中播放 YouTube 视频。我正在使用 YouTubePlayer API,它运行良好。但问题是我想在库中安装此播放器,如果未安装 YouTube 应用程序,则不会显示电影,我还有另一个问题,即在某些旧手机上我需要先升级 YouTube 应用程序。这是其他公司将导入的库我不想依赖 YouTube 应用程序的版本或者用户是否有 YouTube 应用程序。如何优雅地降级以在应用内播放视频?
最佳答案
使用 YouTubeIntents class在 Android Player API 中优雅降级。下面是一些示例代码,用于检测用户可以做什么:
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 分左右开始。
关于android - 如果用户没有安装 YouTube 应用程序,我该如何在 Android 应用程序中播放 YouTube 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254477/