ExoPlayer 读取本地多个音频文件

  • Post author:
  • Post category:其他


private ExoPlayer simpleExoPlayer;
/**
 * 播放一组语音文件
 * @param audioFileNames  
 */
public void playAudiosByExoPlayer(List<String> audioFileNames){

    simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(RKApplication.getInstance(),new DefaultTrackSelector());

    DefaultDataSourceFactory defaultDataSourceFactory =new DefaultDataSourceFactory(RKApplication.getInstance(),"audio/mpeg");

    //创建一个媒体连接源

    ConcatenatingMediaSource concatenatingMediaSource =new ConcatenatingMediaSource();

    //concatenatingMediaSource.addMediaSources(concatenatingMediaSource);

    simpleExoPlayer.setPlayWhenReady(false);

    for(int i=0; i<audioFileNames.size(); i++) {
        //创建一个播放数据源
        MediaItem firstItem = MediaItem.fromUri(audioFileNames.get(i));
        // Add the media items to be played.
        simpleExoPlayer.addMediaItem(firstItem);
    }

    // Prepare the player.
    simpleExoPlayer.prepare();
    // Start the playback.
    simpleExoPlayer.play();
}

最后要记得

    if(simpleExoPlayer != null) {
        simpleExoPlayer.release();//释放相关的资源。
        simpleExoPlayer = null;
    }



版权声明:本文为cw2004100021124原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。