当前我正在使用R1.5.12版本使用自定义UridataSource从InputStream播放MP3文件。我想升级到版本2.7.3,但我不确定哪个类替代了ExtractorSampleSource或是否可以在下面的代码中重复使用我的自定义UridataSource类,名为MyDatasource:
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);
播放具有带有外部数据源的自定义数据源的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);
然后,使用您的自定义DataSourceFactory来创建数据源:
public class CustomDataSourceFactory implements DataSource.Factory {
@Override
public DataSource createDataSource() {
return new CustomDataSource();
}
}
将V1 DataSource
移植到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);