Play Youtube videos in your App

  1. Download youtube player API from https://developers.google.com/youtube/android/player/downloads
  2. Extract the downloaded file and copy “YouTubeAndroidPlayerApi.jar”
  3. Paste “YouTubeAndroidPlayerApi.jar” to your android project “libs” folder
  4. Register your application: open the credential page here https://console.developers.google.com/projectselector2/apis/credentials?supportedpurview=project&project&folder&organizationId
  5. Click on create Project (if you don't have already).
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.FirstProject"
minSdkVersion 16
targetSdkVersion 29
versionCode buildCode
versionName buildName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField 'String', 'YOUTUBE_DEVELOPER_KEY', YOUTUBE_DEVELOPER_KEY
}
}
private fun startYoutubeActivity(videoId: String) {
val intent = YouTubeStandalonePlayer.createVideoIntent(this, BuildConfig.YOUTUBE_DEVELOPER_KEY, videoId)
if (intent.resolveActivity(this.packageManager) != null) {
this.startActivity(intent)
} else {
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=$videoId"))
if (webIntent.resolveActivity(this.packageManager) != null) {
this.startActivity(webIntent)
} else {
// handle the error here
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Rajput

Gaurav Rajput

Working remotely as Android Developer