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:
- Support for Dynamic Adaptive Streaming over HTTP (DASH) and SmoothStreaming, neither of which are are supported by MediaPlayer (it also supports HTTP Live Streaming (HLS), MP4, MP3, WebM, M4A, MPEG-TS and AAC).
- Support for advanced HLS features, such as correct handling of #EXT-X-DISCONTINUITY tags.
- The ability to customize and extend the player to suit your use case. ExoPlayer is designed specifically with this in mind, and allows many components to be replaced with custom implementations.
- Easily update the player along with your application. Because ExoPlayer is a library that you include in your application apk, you have control over which version you use and you can easily update to a newer version as part of a regular application update.
- Fewer device specific issues.
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.
If you are looking for an example project.
I created a simple mediaplayer app with controls that you can find at my Github page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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); } } |
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
May 6, 2016 at 12:16 am
how to play mp3 hls and mp4 with this sample?
May 6, 2016 at 9:20 pm
Hi, in this example, you need a SurfaceView in the activity_main Layout , add the Exoplayer in your build.gradle and add the Internet permission in your Manifest file. Then it should play the file from the Url. If you want to play a local file you have to change Uri.parse(url) to Uri.fromFile(yourfile)