티스토리 뷰

android

ExoPlayer 사용 법.

노명규 2020. 1. 31. 17:05

exoPlayer 장점 - 빠르다. 버벅임이 적다. 가볍다 등등. MediaPlayer보다 훨신 기능도 많고 좋은것 같음.

실제 상용 프로젝트에선 MediaPlayer를 사용한 SurfaceView보다 ExoPlayer가 훨신 좋아보임.. 

 

타겟 환경

compileSdkVersion 22
buildToolsVersion "28.0.3"

minSdkVersion 17

 

 

* build.gradle(Module:app) - android

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}

 

* build.gradle(Module:app) - dependencies

implementation "com.google.android.exoplayer:exoplayer-core:2.7.0"
implementation "com.google.android.exoplayer:exoplayer-ui:2.7.0"

 

 

* build.gradle(Project: app) - buildscript

buildscript {
repositories {
maven { url "https://maven.google.com" }
maven { url "https://jcenter.bintray.com" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}

 

* build.gradle(Project: app) - allprojects

allprojects {
repositories {
maven { url "https://maven.google.com" }
maven { url "https://jcenter.bintray.com" }
}

}

 

*** url이 http이면 에러남. 

원래 google() jcenter() 만 해줘도 되지만.. 구버전을 쓰기 때문에 위와 같이 함. exoPlayer를 2.7.0 버전쓰는 이유도 구버전 컴파일 때문. 

 

 

*xml

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exoPlayerView1"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:use_controller="false"
/>

 

*activity - onCreate()

mExoPlayerView = (PlayerView) findViewById(R.id.exoPlayerView1);
mExoPlayerView.setShutterBackgroundColor(Color.TRANSPARENT);

 

 

*실행 함수 - 매개변수는 R.raw.파일 이름. 

private void exoPlayerStartAnimation(int movie) {
if (mExoPlayer != null) {
mExoPlayer.release();
}


mExoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());


Uri uri = RawResourceDataSource.buildRawResourceUri(movie);
ExtractorMediaSource audioSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(this, "MyExoplayer"),
new DefaultExtractorsFactory(), null, null);

mExoPlayer.prepare(audioSource);
mExoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE);
mExoPlayerView.setPlayer(mExoPlayer);
mExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
mExoPlayer.setPlayWhenReady(true);
}

 

*onDestroy()

if (mExoPlayer != null) {
mExoPlayer.release();
}