/* 본 게시물은 ' ' 의 내용을 토대로 작성되었습니다. */
참고 자료
[안드로이드 Youtube Andoid Player API 적용방법] : https://simsi6.tistory.com/53
[Kotlin을 사용하여 Youtube 플레이어 빌드] :
https://ichi.pro/ko/kotlin-eul-sayonghayeo-youtube-peulleieo-bildeu-194496502378538
1. Youtube Android Player API 다운로드
https://developers.google.com/youtube/android/player/?hl=ko
2. Youtube Android Player API 적용
2-1. Internet 권한 추가
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
2-2. Dependency 추가
참고 자료 [안드로이드 Youtube Andoid Player API 적용방법] 들어가서 jar 형식 Dependency 추가하는 방법 확인.
2-3. YoutubeActivity
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.youtube.player.YouTubeBaseActivity
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerView
private const val YOUTUBE_VIDEO_ID = "RmuL-BPFi2Q" // 영상 ID
class MainActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener {
private val TAG = "YoutubeActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val layout = layoutInflater.inflate(R.layout.activity_main, null) as ConstraintLayout
setContentView(layout)
val playerView = YouTubePlayerView(this)
playerView.layoutParams = ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
layout.addView(playerView)
playerView.initialize(getString(R.string.GOOGLE_API_KEY), this)
}
override fun onInitializationSuccess(provider: YouTubePlayer.Provider?, youTubePlayer: YouTubePlayer?,
wasRestored: Boolean) {
Log.d(TAG, "onInitializationSuccess: provider is ${provider?.javaClass}")
Log.d(TAG, "onInitializationSuccess: youTubePlayer is ${youTubePlayer?.javaClass}")
Toast.makeText(this, "Initialized Youtube Player successfully", Toast.LENGTH_SHORT).show()
youTubePlayer?.setPlayerStateChangeListener(object: YouTubePlayer.PlayerStateChangeListener {
override fun onAdStarted() {
Toast.makeText(this@MainActivity, "Click Ad now, make the video creator rich!", Toast.LENGTH_SHORT).show()
}
override fun onLoading() {
}
override fun onVideoStarted() {
Toast.makeText(this@MainActivity, "Video has started", Toast.LENGTH_SHORT).show()
}
override fun onLoaded(p0: String?) {
youTubePlayer.play() // 동영상을 불러오면 바로 실행
}
override fun onVideoEnded() {
Toast.makeText(this@MainActivity, "Congratulations! You've completed another video.", Toast.LENGTH_SHORT).show()
}
override fun onError(p0: YouTubePlayer.ErrorReason?) {
}
})
youTubePlayer?.setPlaybackEventListener(playbackEventListener)
if (!wasRestored) {
youTubePlayer?.cueVideo(YOUTUBE_VIDEO_ID)
}
}
override fun onInitializationFailure(provider: YouTubePlayer.Provider?,
youTubeInitializationResult: YouTubeInitializationResult?) {
val REQUEST_CODE = 0
if (youTubeInitializationResult?.isUserRecoverableError == true) {
youTubeInitializationResult.getErrorDialog(this, REQUEST_CODE).show()
} else {
val errorMessage = "There was an error initializing the YoutubePlayer ($youTubeInitializationResult)"
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show()
}
}
private val playbackEventListener = object: YouTubePlayer.PlaybackEventListener {
override fun onSeekTo(p0: Int) {
}
override fun onBuffering(p0: Boolean) {
}
override fun onPlaying() {
Toast.makeText(this@MainActivity, "Good, video is playing ok", Toast.LENGTH_SHORT).show()
}
override fun onStopped() {
Toast.makeText(this@MainActivity, "Video has stopped", Toast.LENGTH_SHORT).show()
}
override fun onPaused() {
Toast.makeText(this@MainActivity, "Video has paused", Toast.LENGTH_SHORT).show()
}
}
}
androidx youtube player error
위에 글을 대충 요약하면
1. YoutubePlayerApi 는 jar 형식이다. 이는 별로 문제되지 않지만, 유튜브에서 계속해서 업데이트를 안해주고 있는 오래된 파일이다.
2. 실제로 다른 뷰랑 같이 보이게 하려면 Fragment로 YoutubePlayerView를 사용하게된다.
3. androidx 와 호환불가능하다. 이 문제가 가장 critical 하다. 따라서 아래 URL에 YoutubePlayView를 사용하면 androidx와 사용가능하고 위에 불편한 것을 해결가능하다.
YouTube Player View
관련글
[CardView에 YoutubePlayerView 넣기] : https://nodealdev.tistory.com/72