본문 바로가기
안드로이드 코틀린

[Android][kotlin] youtubePlayer 라이브러리 를 이용해 유튜브영상 표시해 주기

by teamnova 2023. 10. 1.

오늘은 유튜브 영상뷰를 표시해주는 라이브러리를 이용해서 유튜브 영상을 보여주는 뷰를 사용해 보겠습니다. 

 

오늘 사용할 라이브러리 깃허브 링크입니다.

https://github.com/PierfrancescoSoffritti/android-youtube-player

 

GitHub - PierfrancescoSoffritti/android-youtube-player: YouTube Player library for Android and Chromecast, stable and customizab

YouTube Player library for Android and Chromecast, stable and customizable. - GitHub - PierfrancescoSoffritti/android-youtube-player: YouTube Player library for Android and Chromecast, stable and c...

github.com

사용법은 간단합니다.

 

라이브러리 의존성을 입력해 줍니다. 

dependencies {
  implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:12.1.0'
}

이후 레이아웃을 만들어 줍니다. 

 

activity_youtube_view.xml

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityYoutubeView">


    <com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ActivityYoutubeView

package com.example.stickcodepost

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView


class ActivityYoutubeView : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_youtube_view)

        // XML 레이아웃에서 정의된 YouTubePlayerView 요소를 찾아옵니다.
        val youtubePlayerView: YouTubePlayerView = findViewById(R.id.youtube_player_view)

        // 액티비티 또는 프래그먼트의 라이프사이클을 관찰하여
        // YouTubePlayerView를 라이프사이클 이벤트와 동기화하도록 설정합니다.
        lifecycle.addObserver(youtubePlayerView)

        // YouTubePlayerListener를 구현한 익명 클래스를 생성하고,
        // YouTubePlayerView에 이 리스너를 추가합니다.
        youtubePlayerView.addYouTubePlayerListener(object : AbstractYouTubePlayerListener() {

            // YouTube 플레이어가 준비되면 호출되는 콜백 메서드입니다.
            override fun onReady(youTubePlayer: YouTubePlayer) {
                // 재생할 동영상의 ID를 설정합니다.
                val videoId = "B9EsAERudp8"

                // 설정한 동영상 ID로 동영상을 로드하고 재생합니다.
                // 시작 시간을 0초로 설정하여 동영상을 처음부터 재생합니다.
                youTubePlayer.loadVideo(videoId, 0f)
            }
        })

    }


}

사용법은 주석을 참고해 주세요 

 

vidioId 의 경우 유튜브 영상 주소를 보시면
youtube.com/watch?v={비디오 아이디}  <이런 구조로 작성되어 있는데 
비디오 아이디 부분을 복사해서 사용해 주시면 해당 영상을 뷰에 표시해 줄 수 있습니다.