Start playing multiple audio files in the same time in Android with Java

231 views Asked by At

I'm working on music project in Android with Java which I need to start multiple audio files exactly at the same time and I need them to be simultaneously without any gaping even if it's milliseconds

what I tried is :

  • MediaPlayer and I didn't find any way to play the files together
  • SoundPool and I blocked with the limits of 1mb of file sizes
  • ExoPlayer also without a good result
  • MultiThreading and seems not the best way because I have more than 30 sounds need to start at the same time

Please if you worked with anything related or any idea that can help ?

An other note is : what I want exactly is when to start an audio file X1 and make a copy from it and name it X2 , and put two buttons to play X1 and X2 , if I play X1 and after some time play X2 I want them to be in the same currentPosition without pausing , that's why I want to start them in the same time and put their volume to 0 , and then when pressed a button set the volume to 1 , I tried with setCurrentTime and play but it's make a short delay of milliseconds .

thanks for any help

1

There are 1 answers

1
dev.bmax On

AudioTrack is a low-level component that gives you more control over the playback, because you basically push the audio buffers yourself. And there are multiple ways to synchronize them.

In order to use an AudioTrack you have to decode the audio file (probably using the MediaCodec API).

Note that there is a hardcoded limit of 40 AudioTracks that an app can hold simultaneously (see kMaxTracksPerUid defined in services/audioflinger/Threads.h). So if you need to play more then you must implement your own custom audio mixer.