Jens Klingenberg

How to play a mp4 file with Exoplayer

Posted on April 24, 2016  •  3 minutes  • 625 words
Table of contents
Attention: This post may be outdated. It explained Exoplayer v1.x. In v2.x there were many changes to the API. I updated my example mediaplayer app with controls Github page to Exoplayer 2.

What is Exoplayer?

A few weeks ago i needed to implement a video player in an app. At first i used the standard MediaPlayer class from Android. There were enough example projects and it was easy to code a little player, but after a few days i read about that this class isn't really capable of playing adaptive streaming formats like Dash or HLS. So, to be ready for future projects i took a look at ExoPlayer.

ExoPlayer is an Android Library from Google introduced at Google IO 2014. Google uses it in their Youtube app and in the Google Play Movie app. Periscope is also using it. It is much more extensible and customizable than MediaPlayer, but you also need to write more code to be able to play Video/Audio with.

There is a DemoApp in the Github Repo, but at first it was hard for me to figure out what was really necessary to play a simple mp4 file.

Advantages

The advantages from the Google Github Guide ExoPlayer has a number of advantages over Android’s built in MediaPlayer:

Disadvantages

The only disadvantage is that the earliest API level is level 16. But don't think that that is really a problem because below that level there are only 4.9 % Android devices.

How to :

integrate in Android Studio project? you can import the ExoPlayer in your build.gradle file
compile 'com.google.android.exoplayer:exoplayer:rX.X.X'

X.X.X stands for the wanted version You can see the lastet release HERE

prepare player? I figured out that this the only code you need to set up the player. I’m not sure what the perfect buffer and renderer sizes are, but with this values it should work.

public class MainActivity extends AppCompatActivity {

    private ExoPlayer exoPlayer;
    private SurfaceView surfaceView;
    private int RENDERER_COUNT = 300000;
    private int minBufferMs =    250000;
    private final int BUFFER_SEGMENT_SIZE = 64 * 1024;
    private final int BUFFER_SEGMENT_COUNT = 256;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView= (SurfaceView) findViewById(R.id.surfaceView);

        String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:40.0) Gecko/20100101 Firefox/40.0";
        String url = "http://www.sample-videos.com/video/mp4/480/big_buck_bunny_480p_5mb.mp4";
        Allocator allocator = new DefaultAllocator(minBufferMs);
        DataSource dataSource = new DefaultUriDataSource(this, null, userAgent);


        ExtractorSampleSource sampleSource = new ExtractorSampleSource( Uri.parse(url), dataSource, allocator,
                BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);

        MediaCodecVideoTrackRenderer videoRenderer = new
                MediaCodecVideoTrackRenderer(this, sampleSource, MediaCodecSelector.DEFAULT,
                MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);

        MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource, MediaCodecSelector.DEFAULT);

        exoPlayer = ExoPlayer.Factory.newInstance(RENDERER_COUNT);
        exoPlayer.prepare(videoRenderer, audioRenderer);
        exoPlayer.sendMessage(videoRenderer,
                MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
                surfaceView.getHolder().getSurface());
        exoPlayer.setPlayWhenReady(true);

    }
}

If you are looking for an example project. I created a simple mediaplayer app with controls that you can find at my Github page

start playback?

exoPlayer.setPlayWhenReady(true)

stop playback?

exoPlayer.setPlayWhenReady(false)

mute?

exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 0f);

turn sound on again?

exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 1f);

skip to position xy?

exoPlayer.seekTo(xy);


Example Project

If you are looking for an example project. I created a simple mediaplayer app with controls that you can find at my Github page

Sources: https://github.com/google/ExoPlayer Building Periscope for Android

Let's connect: