0

Currently I'm using version r1.5.12 to play MP3 files from InputStream using a custom UriDataSource. I'd like to upgrade to version 2.7.3 but I'm not sure which class replaces ExtractorSampleSource or whether it's possible to reuse my custom UriDataSource class named myDataSource in code below:

int rendererCount=1;

ExoPlayer exoPlayer= ExoPlayer.Factory.newInstance(rendererCount);

/*check if file is present or not*/

File file=new File(getCacheDir(),"sample.mp3"); // location of file in the root directory of SD Card named "sample.mp3"

/*instantiate myDataSource*/
DataSource dataSource=new myDataSource(this);

ExtractorSampleSource extractorSampleSource=new ExtractorSampleSource(Uri.parse("sample.mp3"),dataSource,new DefaultAllocator(64*1024),64*1024*256);
TrackRenderer audio=new MediaCodecAudioTrackRenderer(extractorSampleSource, MediaCodecSelector.DEFAULT,null,true);

/*prepare ExoPlayer*/
exoPlayer.prepare(audio);
exoPlayer.setPlayWhenReady(true);
rraallvv
  • 2,875
  • 6
  • 30
  • 67

1 Answers1

2

To play an MP3 with a custom data source with ExoPlayer 2 you can do something like this:

CustomDataSourceFactory dataSourceFactory = new CustomDataSourceFactory();

File file = new File(getCacheDir(), "sample.mp3");
ExtractorMediaSource mediaSource =
    new ExtractorMediaSource.Factory(dataSourceFactory)
    .createMediaSource(Uri.fromFile(file));

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

Your CustomDataSourceFactory is then used to create data sources:

public class CustomDataSourceFactory implements DataSource.Factory {
  @Override
  public DataSource createDataSource() {
    return new CustomDataSource();
  }
}

Porting your v1 DataSource to v2 should be easy. The only change seems to be the addition of the getUri() method.

Aside: I'm not sure what your custom implementation is actually doing. To just play a file from the cache directory you should be able to do this without the need of a custom data source:

DefaultDataSourceFactory dataSourceFactory =
    new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exo-demo"));
File file = new File(getCacheDir(), "sample.mp3");
ExtractorMediaSource mediaSource =
    new ExtractorMediaSource.Factory(dataSourceFactory)
    .createMediaSource(Uri.fromFile(file));

player.prepare(mediaSource);
player.setPlayWhenReady(true);
marcbaechinger
  • 2,759
  • 18
  • 21