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

为君一笑 提交于 2019-12-05 11:46:55

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.

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