ExoPlayer 2 に関するメモ

2016/9/28 に ExoPlayer r2.0.0 がリリースされました。

また、 ExoPlayer は medium に開発ブログを書くようになったようです。 r2.0.0 がリリースされたのは結構前だけど、未だに追えていなかったので medium の記事を参考にしつつ調べた内容をメモっておきます。

ちなみに ExoPlayer 1.x の話は過去に書いたものもあるので、興味がある方はそちらも参照すると良いでしょう。

Q: なぜメジャーバージョンアップしたの?

A: 後方互換性を破壊したかったから

ExoPlayer 2.x で変わったクラス

ExoPlayer 2.x - New package and class names

再設計するにあたって、複雑なクラスを分解したり、リネームしたものがまとめられている。

MediaSource Composition

ExoPlayer 2.x - MediaSource composition

MergingMediaSource

別々の MediaSource を合成して再生する。以下の場合だと、動画に別ファイルの字幕データをマージしている。

MediaSource videoSource = new ExtractorMediaSource(videoUri, ...);
MediaSource subtitleSource = new SingleSampleMediaSource(subtitleUri, ...);
// Plays the video with the sideloaded subtitle.
MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);

LoopingMediaSource

ConcatenatingMediaSource

New audio features

ExoPlayer 2.x - New audio features

Track Selection

ExoPlayer 2.x - Track selection

SimpleExoPlayer/SimpleExoPlayerView

// TrackSelector, LoadControl を指定して SimpleExoPlayer を作る
Handler mainHandler = new Handler();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
        new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
        new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

// HLS の URL を指定して MediaSource を作る
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
        Util.getUserAgent(this, "TestApp/0.1"), bandwidthMeter);
MediaSource videoSource = new HlsMediaSource(
        Uri.parse(url),
        dataSourceFactory,
        5,
        null,
        null);

// 再生する
player.prepare(videoSource);
player.setPlayWhenReady(true);
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
    android:id="@+id/video"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:use_texture_view="true" />

所感