前言
关于ExoPlayer的使用,很多前人已经帮我们翻译了官方文档,例如ExoPlayer使用,这里不再阐述。
但是上述文章一般就是将官方文档翻译一下,针对具体的使用,可能会遇到很多难以解决问题。
正文
某天,我想用ExoPlayer播放一段存放在res/raw文件夹下的ogg格式的音频,在之前代码中,使用Mediaplayer播放,一切正常。
但是,有了高逼格的ExoPlayer,肯定要放弃Mediaplayer啊,毕竟程序员都是喜新厌旧的嘛(手动滑稽)。所以,那就根据官方API开整,这里就放核心代码
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory selectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(selectionFactory);
currentPlayer = ExoPlayerFactory.newSimpleInstance(this.context, trackSelector);
DataSource.Factory dataSourceFactory = buildDataSourceFactory(this.context, bandwidthMeter);
ExtractorMediaSource mediaSource = new ExtractorMediaSource(FmManager.getUriById(context, resId),
dataSourceFactory, new DefaultExtractorsFactory(), null, null);
LoopingMediaSource loopingMediaSource = new LoopingMediaSource(mediaSource); //循环播放
currentPlayer.prepare(loopingMediaSource);
currentPlayer.addListener(this);
currentPlayer.setPlayWhenReady(true);