ExoPlayer 개요

홍승범·2023년 1월 20일
0

ExoPlayer

목록 보기
1/1

ExoPlayer는 Android 프레임워크에 속하지 않고 Android SDK에서 별도로 배포되는 오픈소스 프로젝트로 Android MediaCodec API를 기반으로 동작한다. MediaPlayer에서 지원하지 않는 DASH(Dynamic adaptive streaming over HTTP), SmoothStreaming 및 일반 암호화 같은 기능을 지원하며, 또 맞춤 설정 및 확장이 용이하도록 설계되었다고 한다.

의존성 설정

라이브러리 전체를 사용하려면 아래와같이 의존성을 추가한다.

implementation 'com.google.android.exoplayer:exoplayer:2.X.X'

일부 기능들만 취사선택하여 사용하려한다면 아래와같이 추가한다. 버전은 반드시 같아야 한다

implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'

플레이어의 생성

ExoPlayer.Builder 클래스를 이용해 생성한다. 다양한 옵션을 쉽게 적용하기 위한 빌더 패턴이다

ExoPlayer player = new ExoPlayer.Builder(context).build();

ExoPlayer의 동작 스레드

ExoPlayer는 단일 애플리케이션 스레드에서만 접근해야 한다. 안드로이드의 경우는 UI스레드가 될것이다.

정확히 말하면 플레이어가 생성되는 스레드의 Looper를 가진 스레드만 접근할 수 있다는것. 만약 Looper가 없는 스레드에서 생성되는경우엔 앱의 Main Looper 가 사용되고 여기서만 접근가능하다. 다음 그림은 ExoPlayer의 스레드 모델을 나타낸다

Playback은 내부의 스레드에서 동작하며 Player의 프론트엔드와는 메시지큐를 통해 정보를 주고 받게된다. 따라서 애플리케이션에서 Player를 컨트롤하여면 반드시 메인스레드에서 동작해야 한다

다른 스레드에서 접근하면 IllegalStateException이 발생하므로, player의 getApplicationLooper메소드를 통해 접근가능한 Looper를 확인해야 한다

플레이어를 뷰에 붙이기

라이브러리에 StyledPlayerView라는 재생 surface, control, subtitleView가 포함된 재생용 뷰를 제공한다. 해당 뷰가 플레이어를 컨트롤 할 수 있도록, 아래와같이 뷰에 플레이어 인스턴스를 제공한다

// Bind the player to the view.
playerView.setPlayer(player);

이외에도 SurfaceView, TextureView, SurfaceHolder 와 Surface 클래스를 이용해 재생렌더링을 할 수 있다. 이건 너무 당연한가?

플레이리스트, 재생 아이템

기존 MediaPlayer는 setDataSource를 이용해 uri나 File Descriptor를 전달했다. ExoPlayer는 재생하기위한 아이템의 단위인 MediaItem을 제공한다. 기본적인 사용 방법은 아래와 같다

// Build the media item.
MediaItem mediaItem = MediaItem.fromUri(videoUri);
// Set the media item to be played.
player.setMediaItem(mediaItem);
// Prepare the player.
player.prepare();
// Start the playback.
player.play();

MediaPlayer와 다른점은 플레이리스트를 player에 직접 전달하고, 그것을 플레이어가 관리할 수 있다는 점이다. MediaPlayer는 리스트관리를 개발자가 직접하고, 하나의 컨텐츠가 재생이 끝날 때 다음 아이템을 직접 setDataSource해 주어야 했었다. ExoPlayer는 다음과 같이 처리한다

// Build the media items.
MediaItem firstItem = MediaItem.fromUri(firstVideoUri);
MediaItem secondItem = MediaItem.fromUri(secondVideoUri);
// Add the media items to be played.
player.addMediaItem(firstItem);
player.addMediaItem(secondItem);
// Prepare the player.
player.prepare();
// Start the playback.
player.play();

play를 하고 나면 하나가 재생이 끝난 후 바로 다음아이템을 알아서 재생해준다....!.!!!!!

컨트롤

기본적인 컨트롤은 MediaPlayer와 같다. play / pause / seekTo / repeatMode 등을 지원하며, playlist관련 동작들 (has)previous/next, shuffle, 재생속도나 오디오 피치 조절을 위한 파라미터들을 지정할 수도 있다.

플레이어가 StyledPlayerView 나 StyledPlayerControlView 에 바인딩되어있다면, 뷰를 통해 유저가 컨트롤을 연계할 수 있다.(직접 뷰를 다 안만들어도 어느정도 된다는것)

해제

플레이어를 더이상 사용하지 않으면 video decoder 같은 자원을 반환하기 위해 반드시 release를 해 주어야 한다. release 메소드를 호출함으로써 해제할 수 있다.


https://exoplayer.dev/hello-world.html
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ExoPlayer.html
https://developer.android.com/guide/topics/media/exoplayer

profile
그냥 사람

0개의 댓글