Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top