Side Project - Youtube
영상의 목록을 보여주고 영상을 플레이하는 유튜브의 간단한 기능을 구현한 프로젝트
- Github -> https://github.com/hwayeon351/Youtube
GitHub - hwayeon351/Youtube
Contribute to hwayeon351/Youtube development by creating an account on GitHub.
github.com
학습 회고
오늘은 ExoPlayer를 사용해서 영상 리스트 중 하나를 선택하면 영상을 플레이하고 멈추고, 종료하는 로직을 구현하였다.
오늘 공부한 내용
- ExoPlayer
ExoPlayer는 Android SDK에서 별도로 배포되는 오픈 라이브러리이다.
ExoPlayer는 안드로이드 환경에서 미디어 플레이어 기능을 제공한다.
안드로이드의 MediaPlayer와 달리 ExoPlayer는 인터넷을 통해 오디오나 비디오를 플레이할 수 있는 기능을 제공한다.
또한, 쉽게 커스터마이징하고 확장할 수 있다는 장점을 가지고 있다.
- 앱 모듈 단위의 Gradle 파일에 의존성 추가하기
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
- 레이아웃에 PlayerView 추가하기
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/mainContainerLayout"
app:layout_constraintStart_toStartOf="@id/mainContainerLayout"
app:layout_constraintTop_toTopOf="@id/mainContainerLayout"
app:resize_mode="fill" />
- PlayerView에 ExoPlayer 인스턴스 붙이기
context?.let {
player = SimpleExoPlayer.Builder(it).build()
}
fragmentPlayerBinding.playerView.player = player
- MediaItem 생성하고 Player로 재생하기
ExoPlayer에서는 모든 미디어는 MediaItem으로 표현된다.
미디어를 재생하려면 MediaItem을 빌드하고 Player에 추가해서 Player를 준비한 후, play()를 호출해서 재생할 수 있다.
내부적으로 Player는 콘텐츠를 재생하기 위해 MediaSource 인스턴스가 필요하다.
Player는 MediaSource.Factory를 사용해서 Media Item에서 MediaSource를 생성한다.
val dataSourceFactory = DefaultDataSourceFactory(it)
val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(Uri.parse(url)))
player?.setMediaSource(mediaSource)
player?.prepare()
player?.play()
디폴트로 Player에서는 DefaultMediaSourceFactory를 사용한다.
유튜브 앱에서는 MP4 영상으로 재생할 것이기 때문에 DefaultDataSourceFactory에서 ProgressiveMediaSource로 바꾸어 사용하였다.
ProgressiveMediaSource 인스턴스를 비디오 URL을 담은 MediaItem을 넣어서 생성해주었다.
* Progressive에서 지원하는 포맷을 확인하려면 다음 링크를 참고하면 된다.
https://exoplayer.dev/progressive.html
Progressive - ExoPlayer
exoplayer.dev
https://exoplayer.dev/media-sources.html
Media sources - ExoPlayer
exoplayer.dev
https://exoplayer.dev/hello-world.html
Hello world! - ExoPlayer
exoplayer.dev
'Android > Side Projects' 카테고리의 다른 글
음악 스트리밍 앱 개발하기 (0) | 2022.03.10 |
---|---|
음악 스트리밍 앱 개발하기 (1) | 2022.03.10 |
Youtube 앱 개발하기 (0) | 2022.03.03 |
Youtube 앱 개발하기 (0) | 2022.03.03 |
Youtube 앱 개발하기 (0) | 2022.03.01 |