- 구글 ExoPlayer
https://github.com/google/ExoPlayer
https://developer.android.com/guide/topics/media/exoplayer
- 기본 미디어 플레이와 HLS 미디어 플레이 예제
1. 예제코드
- 어플리케이션 build.gradle 설정
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
- 기본코드
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static class EventListener extends Player.DefaultEventListener {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
String stateString;
switch (playbackState) {
case Player.STATE_IDLE:
stateString = "ExoPlayer.STATE_IDLE";
break;
case Player.STATE_BUFFERING:
stateString = "ExoPlayer.STATE_BUFFERING";
break;
case Player.STATE_READY:
stateString = "ExoPlayer.STATE_READY";
break;
case Player.STATE_ENDED:
stateString = "ExoPlayer.STATE_ENDED";
break;
default:
stateString = "UNKNOWN_STATE";
break;
}
Log.d(TAG, "changed state to " + stateString + ", playWhenReady: " + playWhenReady);
}
}
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private SimpleExoPlayer exoPlayer;
private long playbackPosition;
private int currentWindow;
private boolean playWhenReady = true;
private EventListener eventListener = new EventListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
initPlayer();
}
@Override
protected void onPause() {
super.onPause();
releasePlayer();
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
private void initPlayer() {
String url = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8";
PlayerView playerView = findViewById(R.id.playerView);
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(adaptiveTrackSelectionFactory), new DefaultLoadControl());
exoPlayer.addListener(new EventListener());
MediaSource mediaSource = url.endsWith(".m3u8") ? buildMediaSourceHLS(Uri.parse(url)) : buildMediaSourceVideo(Uri.parse(url));
exoPlayer.prepare(mediaSource, true, false);
exoPlayer.setPlayWhenReady(playWhenReady);
exoPlayer.seekTo(currentWindow, playbackPosition);
playerView.setPlayer(exoPlayer);
}
private void releasePlayer() {
if (exoPlayer != null) {
playbackPosition = exoPlayer.getCurrentPosition();
currentWindow = exoPlayer.getCurrentWindowIndex();
playWhenReady = exoPlayer.getPlayWhenReady();
exoPlayer.removeListener(eventListener);
exoPlayer.release();
exoPlayer = null;
}
}
private MediaSource buildMediaSourceHLS(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(manifestDataSourceFactory).createMediaSource(uri);
return new ConcatenatingMediaSource(hlsMediaSource);
}
private MediaSource buildMediaSourceVideo(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory(TextUtils.isEmpty(userAgent) ? "DefaultHttpDataSourceFactory" : userAgent)).
createMediaSource(uri);
return new ConcatenatingMediaSource(videoSource);
}
}
> buildMediaSourceHLS 메소드는 HLS 미디어를 재생하기위한 메소드
> 예제에서는 PlayerView를 통해 ExoPlayer를 재생하는 예제.
> 안드로이드 MediaPlayer과 동일하게 구현 가능.
> 주요 특징은 별도 검색으로 확인해 볼 것.
> 사용된 HSL 미디어 소스
https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8
추가) 오디오와 영상이 분리된 경우
private MediaSource buildMediaSourceAdaptive(Uri video, Uri audio) {
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(video);
ExtractorMediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(audio);
MergingMediaSource mergingMediaSource = new MergingMediaSource(videoSource, audioSource);
return new ConcatenatingMediaSource(mergingMediaSource);
}
2. 샘플코드
소스코드
APK
댓글 없음:
댓글 쓰기