使用ExoPlayer播放res/raw中音频资源

前言

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


    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值