通过ExoPlayer或其他一些选项播放多个音频文件?

时间:2019-07-11 13:31:54

标签: android media-player soundpool exoplayer2.x

我的问题:我想在应用中加载70种声音,然后播放多种声音(一次最多8种声音)。示例应用如下: https://play.google.com/store/apps/details?id=net.relaxio.sleepo

我在我的应用程序中尝试了SoundPool系统,这使我在加载时遇到问题。 SoundPool加载70个文件大约需要60秒钟以上。因此,此选项对我的应用程序不利。 然后,我尝试MediaPlayer。 MediaPlayer可以加载,但是会带来另一个问题。当我在mediaplayer中设置循环时,它会在重启之间留出间隔。 然后我发现了一个github项目,一次可以播放多种声音,而且加载速度很快,但是这个项目仍然有问题。无法正确加载30多个声音。在这个项目中,他们使用ExoPlayer(下面是链接)。 https://github.com/zoenb/Soft-Sound

请帮助我,我该怎么办才能解决声音和环路间隙问题。

1 个答案:

答案 0 :(得分:3)

是的,您必须创建exoplayer的多个(8)实例,而不能只用一个实例

这是我的代码,

    private void initPlayer() {
      if (player == null) {
        trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
        TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
        trackSelector = new DefaultTrackSelector(trackSelectionFactory);
        trackSelector.setParameters(trackSelectorParameters);

        RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();

        player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);

        player.setPlayWhenReady(true);

        //to print log of ExoPlayer
        //player.addAnalyticsListener(new EventLogger(trackSelector));

        progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);

      }
    }

使用上述代码初始化exoplayer并调用此方法以添加要同时播放的所有8个URI

 public void startPlaying(Uri uri) {
        if (player == null) initPlayer();
        MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
        player.prepare(mediaSource, true, false);
  }

这是Github链接-https://github.com/google/ExoPlayer

使用exoplayer-core

为此,我建议您为ExoPlayer制作一个单独的类,以进行初始化,启动,停止等等……

public class MyAppExoPlayer {

private final Context context;


private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player;
private DefaultTrackSelector trackSelector;
private DefaultTrackSelector.Parameters trackSelectorParameters;


private ExoPlayerStateListener exoPlayerStateListener;
private ProgressiveMediaSource.Factory progressiveMediaSourceFactory;


private ConcatenatingMediaSource concatenatingMediaSource;
private int currentMediaPlayerIndex = 0;

public MyAppExoPlayer(Context context, ExoPlayerStateListener exoPlayerStateListener) {
    this.context = context;
    this.exoPlayerStateListener = exoPlayerStateListener;

    dataSourceFactory = ((MyAppApplication) context.getApplicationContext()).buildDataSourceFactory();

    initPlayer();
}

private void initPlayer() {
    if (player == null) {
        trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
        TrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
        trackSelector = new DefaultTrackSelector(trackSelectionFactory);
        trackSelector.setParameters(trackSelectorParameters);

        RenderersFactory renderersFactory = ((MyAppApplication) context.getApplicationContext()).buildRenderersFactory();

        player = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);

        player.setPlayWhenReady(true);

        //to print log of ExoPlayer
        //player.addAnalyticsListener(new EventLogger(trackSelector));

        progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);

    }

    concatenatingMediaSource = new ConcatenatingMediaSource();
}

public void startPlaying(Uri uri) {
    if (player == null) initPlayer();
    MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
    player.prepare(mediaSource, true, false);
}

public void addToQ(Uri uri) {
    if (player == null) return;
    MediaSource mediaSource = progressiveMediaSourceFactory.createMediaSource(uri);
    concatenatingMediaSource.addMediaSource(mediaSource);
}

public void stopPlaying() {
    if (player != null) {
        player.stop(true);
        concatenatingMediaSource.clear();
    }
}

public long getCurrentPosition() {
    if (player != null)
        return player.getCurrentPosition();
    return 0;
}

public void releasePlayer() {
    if (player != null) {

        if (trackSelector != null) {
            trackSelectorParameters = trackSelector.getParameters();
        }

        player.release();
        player = null;
        trackSelector = null;
    }
}
}

如果MyAppExoPlayer现在创建8个实例并调用startPlaying()