Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple instance of ExoPlayer

I am currently looking to develop an application that utilises Dash through the ExoPlayer in Android.

To begin with I am going through the demo project however am having trouble with even creating a simple working instance of ExoPlayer that can stream mp3 or similar.

Would really appreciate any help anyone can give relating to getting a very simple exoplayer instance working from which i can adapt and build upon or if anyone has any leads for more references or guides which I can follow as there seems to be very little documentation available.

Thanks very much for all and any help!

like image 909
Max Marshall Avatar asked Jun 25 '15 11:06

Max Marshall


Video Answer


2 Answers

Today while working on a project, I found that this.myExoPlayer = ExoPlayerFactory.newSimpleInstance(getActivity()); and some others are now deprecated and Android studio suggested to use new way. So I did a quick Google for it but everywhere I found the old way. So I looked into the SimpleExoPlayer.java file, read some methods. So this is how you initialize simpleExoPlayer:

  Activity activity = getActivity(); // if you are in a fragment
  // Or,   activity = YourActivity.this;      if you are in an Activity
  SimpleExoPlayer simpleExoPlayer = new SimpleExoPlayer.Builder(activity).build();

I hope this is helpful.

like image 186
Qazi Fahim Farhan Avatar answered Oct 19 '22 19:10

Qazi Fahim Farhan


Here is how you would do it using the new ExoPlayer 2 API, and the SimpleExoPlayer.

First create the player:

DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, bandwidthMeter);

TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
DefaultTrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();

SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);

player.addListener(...); // To receive events from the player

Then create your MediaSource. For MP3 you can use ExtractorMediaSource:

ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
Uri uri = Uri.parse(mp3UriString);
Handler mainHandler = new Handler(Looper.getMainLooper());
MediaSource mediaSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, mainHandler, mediaSourceListener); // Listener defined elsewhere

Then prepare and play when ready:

player.prepare(mediaSource);
player.setPlayWhenReady(true);

For DASH you would use DashMediaSource instead of ExtractorMediaSource.

like image 42
Samuel Peter Avatar answered Oct 19 '22 21:10

Samuel Peter