11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ExoPlayer(ver2)をとりあえず動かしたい人用全部デフォルトコード

Posted at

詳しい説明はこちら

とりあえずガイドを翻訳したのは上にあげたんですけどそんなのいいから動かしてみたいって人いるきがしたので(というか一番最初に僕が思ったことなので)ExoPlayerのVer2を一番簡単に動かすコード上げます。mp4とかmp3とか再生するだけならこれで十分じゃないですかね。あとはTextureView改造すればいい気がします。

最後Textureviewに入れてますが、このようにonFurfaceTextureAvailableより後でやらないと確かできなかった気がします。surfaceを設定することもできた気がします。


 @Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

           DefaultBandwidthMeter defaultBandwidthMeter =new DefaultBandwidthMeter();
            TrackSelection.Factory videoTrackSelectionFactory =
                    new AdaptiveVideoTrackSelection.Factory(defaultBandwidthMeter);
            TrackSelector trackSelector =
                    new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);

            // 2. Create a default LoadControl
            LoadControl loadControl = new DefaultLoadControl();

            // 3. Create the player
            player =
                    ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);

            // Produces DataSource instances through which media data is loaded.
            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                    Util.getUserAgent(context, context.getString(R.string.app_name)), defaultBandwidthMeter);

            // Produces Extractor instances for parsing the media data.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();


            // This is the MediaSource representing the media to be played.
            MediaSource videoSource = new ExtractorMediaSource([再生したいメディアのUri(Uri.parse(URL文字列)でおけ)],
                    dataSourceFactory, extractorsFactory, null, null);



            // Prepare the player with the source.
            player.prepare(videoSource);

            player.setVideoTextureView(textureView);

}

ちなみにこれTextureView使ってますがもっと簡単な方法でSimpleExoPlayerViewを使う方法があります。再生ボタンとかまでぜんぶ作ってくれます。再生ボタンとか無効にして自分のオリジナルボタン作るのでもいいかもです。

xml

  <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/playerView"/>

って適当にxmlに専用のViewが置けて、さっきのコードの最後を


 player.prepare(videoSource); 

 SimpleExoPlayerView playerView =findViewById(R.id.playerView);

 playerView.setPlayer(player);

ってするだけです。なんて簡単。

で、ちなみになんですが、ExoPlayerは全部Javaで書かれてるからいろいろなところを変更できるよ!!って言ってたのでいいね!と思ったのですが、interface以外のクラスが全部finalになってまして...(これって普通なのかな...)

あーちょっとここ変えたいなぁオーバーライドしよ!!っておもってやってみると、それ以外のロードだったりレンダリングだったりの部分の膨大なコードを自分で実装しなきゃいけなかったりします。このデフォルトコード以外で簡単に変更できるのは正直bandwidthmeterだけなんじゃないかなぁとか思いました。
作者は正しい方向で使ってもらうために全部finalにしたよ!!みたいなこと言ってましたが...それっていいことなんですかね。そこら辺の常識に疎いです。

まあなのでよほどの変なことしないかぎりこのコードで十分な気もします。(僕は今そのよほど変なことをスルために苦戦してます。技術力がないだけな気がしますが。)

11
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?