How do I play YouTube videos in an Android app if a user does not have the YouTube app installed?

雨燕双飞 提交于 2020-01-23 08:00:29

问题


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?


回答1:


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

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



来源:https://stackoverflow.com/questions/21254477/how-do-i-play-youtube-videos-in-an-android-app-if-a-user-does-not-have-the-youtu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!