Media Streaming With Exoplayer
The latest player to play on mobile for music song i.e. Exoplayer
Exoplayer is an application level Media Player Library.ExoPlayer standard audio and video components are built on Android’s MediaCodec API, which was released in Android 4.1 .It is an adaptive audio and video streaming.Because ExoPlayer is a library, you can easily take advantage of new features as they become available by updating your app.I have been use MediaPlayer to stream a song but problem is song stream in approx all mobile except redemii and latest android 7 mobile thats why we use Exoplayer to stream song ,when we use Exoplayer to stream audio and video song there is no problem to play song.Exoplayer is very fast and smooth audio and video songs.
Feature of Exoplayer:
1>Exoplayer is an application level Media Player Library.
2>It works on all mobile to stream audio and video songs.
3>It is very fast and smooth player to stream an audio and video songs.
4>Ability to run the very same Player version on all devices.
How to use Exoplayer :
1>First add dependency in build.gradle:
dependencies {
compile ‘com.google.android.exoplayer:exoplayer:r1.5.3’
}
It is very important to allow import exoplayer in your activity.
2> Inintialise Exoplayer in onCreate method
ExoPlayer exoplayer;//It is declare globally in your activity.
exoplayer = ExoPlayer.Factory.newInstance(1);
In your activity First initialize Exoplayer in onCreate method
3>Write this code where you want to stream audio player
MediaCodecAudioTrackRenderer audioRenderer = null;
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(localContext, “ExoPlayerDemo”);
DataSource dataSource = new DefaultUriDataSource(localContext, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(Uri.parse(_uri),
dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
mediaPlayer.prepare(audioRenderer);
mediaPlayer.setPlayWhenReady(true);
Where _uri is Url of songs.
Method To Play ExoPlayer
public void playMedia() {
if (!mediaPlayer.getPlayWhenReady()) {
isPlaying = true;
isPause = false;
mediaPlayer.setPlayWhenReady(true);
}
}
This is the method to call for stream a song and play.Inwhich isPlaying and isPause is a boolean value.
Method to pause media player:
public void pauseMedia() {
if (mediaPlayer.getPlayWhenReady()) {
isPlaying = false;
isPause = true;
mediaPlayer.setPlayWhenReady(false);
}
}
This is the method to call for pause a song.Inwhich isPlaying and isPause is a boolean value.
Method to stop media player
if (mediaPlayer.getPlayWhenReady()) {
mediaPlayer.stop();
}
This is the method to call for stop a song.
Conclusion:
Exoplayer is a powerful concept to stream an audio and video songs.It is very fast and smooth player.When we use Exoplayer Stream an audio and video song on all mobile,there is no problem in any mobile in stream a song.