ExoPlayer r1.5.12 到 2.7.3 迁移

ExoPlayer r1.5.12 to 2.7.3 migration

目前我正在使用 r1.5.12 版本使用自定义 UriDataSource 播放来自 InputStream 的 MP3 文件。我想升级到版本 2.7.3,但我不确定哪个 class 替换了 ExtractorSampleSource 或者是否可以在下面的代码中重用我的名为 myDataSource 的自定义 UriDataSource class:

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);

要使用 ExoPlayer 2 播放带有自定义数据源的 MP3,您可以这样做:

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);

然后使用您的 CustomDataSourceFactory 创建数据源:

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

移植您的 v1 DataSource to v2 应该很容易。唯一的变化似乎是添加了 getUri() 方法。

旁白:我不确定您的自定义实现实际上在做什么。要仅播放缓存目录中的文件,您应该能够在不需要自定义数据源的情况下执行此操作:

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);