with YouTube data API
dependencies {
...
implementation files('libs/YouTubeAndroidPlayerApi.jar')
}
<uses-permission android:name="android.permission.INTERNET"/>
dependencies {
...
implementation 'com.squareup.retrofit2:retrofit:2.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
}
data class Video(val items: List<VideoMeta>)
data class VideoMeta(
val id: String,
val snippet: VideoSnippet,
val statistics: VideoStatistics
)
class VideoSnippet(
val publishedAt: String, val title: String, val thumbnails: VideoThumbnail
)
class VideoThumbnail(val default:ThumbnailURL, val medium:ThumbnailURL, val high:ThumbnailURL)
class ThumbnailURL(val url: String, val width: UInt, val height: UInt)
class VideoStatistics(val viewCount: Long)
interface RetrofitYouTubeService {
@GET("/youtube/v3/videos")
fun requestVideoInformation(
@Query("id") videoId: String,
@Query("key") developerKey: String = "YOUR_API_KEY",
@Query("fields") fields: String = "items(id,snippet(publishedAt,title,thumbnails),statistics(viewCount))",
@Query("part") part: String = "snippet,statistics"
): Call<Video>
}
object RetrofitYouTubeData {
private var instance: Retrofit? = null
private const val BASE_URL = "https://www.googleapis.com"
// SingleTon
fun getInstance(): Retrofit {
if (instance == null) {
instance = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return instance!!
}
}
class MainActivity : AppCompatActivity() {
var mDataHandler: DataHandler? = null
private lateinit var retrofit: Retrofit
private lateinit var youtubeService: RetrofitYouTubeService
lateinit var bmp: Bitmap
lateinit var videoTitle: String
lateinit var uploadDate: String
lateinit var previewImageView: ImageView
lateinit var titleTextView: TextView
lateinit var dateTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mDataHandler = DataHandler()
retrofit = RetrofitYouTubeData.getInstance()
youtubeService = retrofit.create(RetrofitYouTubeService::class.java)
previewImageView = findViewById<ImageView>(R.id.previewIv)
titleTextView = findViewById<TextView>(R.id.titleTv)
dateTextView = findViewById<TextView>(R.id.dateTv)
loadData()
}
fun loadData() {
//https://www.youtube.com/watch?v=dyRsYk0LyA8
youtubeService.requestVideoInformation(
"dyRsYk0LyA8"
).enqueue(object : Callback<Video> {
override fun onFailure(call: Call<Video>, t: Throwable) {
}
override fun onResponse(call: Call<Video>, response: Response<Video>) {
if (response.isSuccessful) {
val result = response.body()
val localDate: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
val date: Date = localDate.parse(result?.items?.get(0)?.snippet?.publishedAt)
val uploadDateFormat = SimpleDateFormat("yyyy-MM-dd").format(date)
videoTitle = result?.items?.get(0)?.snippet?.title!!
uploadDate = uploadDateFormat.toString()
Thread(Runnable {
while (true) {
if (result?.items?.get(0)?.snippet?.thumbnails?.high?.url != null) {
bmp = BitmapFactory.decodeStream(
URL(result?.items?.get(0)?.snippet?.thumbnails?.high?.url).openConnection()
.getInputStream()
)
}
if (bmp != null) {
break
}
}
mDataHandler?.sendEmptyMessage(1)
}).start()
}
}
})
}
inner class DataHandler : Handler() {
override fun handleMessage(msg: Message) {
previewImageView.setImageBitmap(bmp)
titleTextView.text = videoTitle
dateTextView.text = uploadDate
}
}
}
youtubeService.requestVideoInformation("dyRsYk0LyA8")