Skip to content

Instantly share code, notes, and snippets.

@reuniware
Last active February 12, 2020 15:39
Show Gist options
  • Select an option

  • Save reuniware/8a0fb049f180536af173172085f35255 to your computer and use it in GitHub Desktop.

Select an option

Save reuniware/8a0fb049f180536af173172085f35255 to your computer and use it in GitHub Desktop.
Android + Kotlin + YouTube API : Get playlist basic code
// Largely inspired by sourcecode at : https://github.com/akoscz/YouTubePlaylist/blob/master/app/src/main/java/com/akoscz/youtube/GetPlaylistAsyncTask.java
package com.reuniware.humoristesfr
import android.os.Bundle
import android.os.StrictMode
import android.text.TextUtils
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.api.client.extensions.android.http.AndroidHttp
import com.google.api.client.json.gson.GsonFactory
import com.google.api.services.youtube.YouTube
import com.google.api.services.youtube.model.PlaylistItemListResponse
import com.google.api.services.youtube.model.VideoListResponse
import java.io.IOException
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mJsonFactory = GsonFactory()
val mTransport = AndroidHttp.newCompatibleTransport()
val mYouTubeDataApi = YouTube.Builder(mTransport, mJsonFactory, null)
.setApplicationName("Humoristes FR")
.build()
val YOUTUBE_API_KEY = "PUT YOUR YOUTUBE API KEY HERE"
val YOUTUBE_PLAYLIST_MAX_RESULTS = 50L; // 50 max
//see: https://developers.google.com/youtube/v3/docs/playlistItems/list
val YOUTUBE_PLAYLIST_PART = "snippet";
val YOUTUBE_PLAYLIST_FIELDS = "pageInfo,nextPageToken,items(id,snippet(resourceId/videoId))";
//see: https://developers.google.com/youtube/v3/docs/videos/list
val YOUTUBE_VIDEOS_PART = "snippet,contentDetails,statistics"; // video resource properties that the response will include.
val YOUTUBE_VIDEOS_FIELDS = "items(id,snippet(title,description,thumbnails/high),contentDetails/duration,statistics)"; // selector specifying which fields to include in a partial response.
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val playlistItemListResponse: PlaylistItemListResponse
playlistItemListResponse = try {
mYouTubeDataApi.playlistItems()
.list(YOUTUBE_PLAYLIST_PART)
.setPlaylistId("PLvaYnFuMvAlY6LEeNkUTutxFJqxjDo2BR") // Put your YouTube playlist id here
.setPageToken("")
.setFields(YOUTUBE_PLAYLIST_FIELDS)
.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)
.setKey(YOUTUBE_API_KEY)
.execute()
} catch (e: IOException) {
e.printStackTrace()
return
}
//Toast.makeText(this, playlistItemListResponse.toString(), Toast.LENGTH_LONG).show()
val videoIds = ArrayList<String>()
playlistItemListResponse.items.forEach{
//Toast.makeText(this, it.snippet.resourceId.videoId, Toast.LENGTH_LONG).show()
videoIds.add(it.snippet.resourceId.videoId)
}
val videoListResponse: VideoListResponse
videoListResponse = try {
mYouTubeDataApi.videos()
.list(YOUTUBE_VIDEOS_PART)
.setFields(YOUTUBE_VIDEOS_FIELDS)
.setKey(YOUTUBE_API_KEY)
.setId(TextUtils.join(",", videoIds))
.execute()
} catch (e: IOException) {
e.printStackTrace()
return
}
videoListResponse.forEach{
Toast.makeText(this, it.value.toString(), Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment