Search code examples
androidmedia-playersoundpoolexoplayer2.x

Play Multiple audio files through ExoPlayer or some other option?


My problem: I want to load 70 sounds in my app and then want to play multiple sound(maximum 8 sound at a time). Example app is below: https://play.google.com/store/apps/details?id=net.relaxio.sleepo

I try SoundPool system in my app and it give me a problem in loading. SoundPool take about more then 60 seconds for loading 70 files. So this option is not good for my app. Then i try MediaPlayer. MediaPlayer is ok with loading but give another problem. When i set looping in mediaplayer then it give a gap between restarting. Then i found a github project that play multiple sound at a time and also the loading is fast but still i have problem with this project. It can't load more then 30 sounds properly. In this project they use ExoPlayer(link is below). https://github.com/zoenb/Soft-Sound

Please help me what can i do so that the loading of sound and loop gap problem to be solved.


Solution

  • yes, you have to create multiple(8) instances of exoplayer, you can't do it with one instance

    here is my code,

        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);
    
          }
        }
    

    use above code to initialize exoplayer and call this method to add all the 8 URIs to play at the same time

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

    here is the Github link - https://github.com/google/ExoPlayer

    use exoplayer-core library

    for that, I would like to suggest you make a separate class to ExoPlayer to initialize, start, stop and all like...

    public class MyAppExoPlayer {
    
    private final Context context;
    
    
    private DataSource.Factory dataSourceFactory;
    private SimpleExoPlayer player;
    private DefaultTrackSelector trackSelector;
    private DefaultTrackSelector.Parameters trackSelectorParameters;
    
    
    private ProgressiveMediaSource.Factory progressiveMediaSourceFactory;
    
    
    private ConcatenatingMediaSource concatenatingMediaSource;
    private int currentMediaPlayerIndex = 0;
    
    public MyAppExoPlayer(Context context) {
        this.context = context;
    
        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;
        }
    }
    }
    

    Now create 8 instances if MyAppExoPlayer and call startPlaying()