6

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40
  • 1
    Are your sounds embedded within your application or do you have to download them first? How long are your sounds? I'm actually using several Exoplayer-instances within a Service in my app, and I can even play more than eight sounds at a time. (no looping, but restart is possible). check here: https://play.google.com/store/apps/details?id=de.jahpress.android – longi Jul 18 '19 at 13:54
  • no issue with 8 sounds a time. issue of exoplayer occur when instances is more then 35. i have used it for 70 sounds. so it work upto 35 but after it it con't play some sounds – ibad ur rahman Jul 19 '19 at 06:36
  • if you only want to play maximum 8 sounds at a time, i would suggest that you should have 8 instances of exoplayer and switch the sounds instead of keeping an instance for every sound. if i understood correctly – longi Jul 19 '19 at 10:23

1 Answers1

4

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()

Priyanka
  • 3,369
  • 1
  • 10
  • 33