如果用户未安装YouTube应用,如何在Android应用中播放YouTube视频? [英] How do I play YouTube videos in an Android app if a user does not have the YouTube app installed?

查看:248
本文介绍了如果用户未安装YouTube应用,如何在Android应用中播放YouTube视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用中播放YouTube视频.我正在使用YouTubePlayer API,效果很好.但是问题是我想在该库中安装此播放器,并且如果未安装YouTube应用,则电影将不会显示,并且还有另一个问题,在某些旧手机上,我需要先升级YouTube应用.这是其他公司将导入的库,我不想依赖于YouTube应用程序的版本,也不希望用户拥有YouTube应用程序.如何优雅降级以在应用程序中播放视频?

I am trying to play YouTube videos in an Android app. I am using YouTubePlayer API and it works well. But the problem is that I want to have this player in the library and if YouTube app is not installed then the movie is not shown and I also have another problem that on some old phones I need to upgrade the YouTube app first. This is a library that other companies will import I do not want to be dependent on version of YouTube app or if user has the YouTube app. How do I gracefully degrade to play videos within the app?

推荐答案

使用

Use the YouTubeIntents class in the Android Player API to gracefully degrade. Here's some sample code to detect what the user can do:

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

如果您好奇的话,我们会在我们从2013年开始的Google I/O对话中逐步介绍此代码.从6:17左右开始.

We walk through this code in our Google I/O talk from 2013, if you're curious. Start from around 6:17.

这篇关于如果用户未安装YouTube应用,如何在Android应用中播放YouTube视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆