Android ExoPlayer Using ExoPlayer

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Instantiate your ExoPlayer:

exoPlayer = ExoPlayer.Factory.newInstance(RENDERER_COUNT, minBufferMs, minRebufferMs);

To play audio only you can use these values:

RENDERER_COUNT = 1 //since you want to render simple audio
minBufferMs = 1000 
minRebufferMs = 5000

Both buffer values can be tweaked according to your requirements.

Now you have to create a DataSource. When you want to stream mp3 you can use the DefaultUriDataSource. You have to pass the Context and a UserAgent. To keep it simple play a local file and pass null as userAgent:

DataSource dataSource = new DefaultUriDataSource(context, null);

Then create the sampleSource:

ExtractorSampleSource sampleSource = new ExtractorSampleSource(
                uri, dataSource, new Mp3Extractor(), RENDERER_COUNT, requestedBufferSize);

uri points to your file, as an Extractor you can use a simple default Mp3Extractor if you want to play mp3. requestedBufferSize can be tweaked again according to your requirements. Use 5000 for example.

Now you can create your audio track renderer using the sample source as follows:

MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);

Finally call prepare on your exoPlayer instance:

exoPlayer.prepare(audioRenderer);

To start playback call:

exoPlayer.setPlayWhenReady(true);


Got any Android Question?