Multiple audio tracks, single stream or multiple?

In my application, I will create several audio tracks, some of which will play at the same time. I am going to use MODE_STREAM and write data as the application starts. Sounds will be dynamically generated, so I use Audio Track, and not something else. I have 4 options, I think:

  • AsyncTask
  • One UI stream and one stream that controls AudioTrack playback
  • One UI thread and one thread for each AudioTrack
  • One UI Thread and Thread Pool

Which of the four methods would be the best way to manage multiple AudioTracks?

I think the thread pool is the way to go, but I'm not sure, since I didn't actually use it.

Any advice would be greatly appreciated.

+4
source share
3 answers

For simplicity, I just created a new stream every time I played AudioTrack.

But despite this, it does not matter. I found that when I try to play multiple AudioTrack at a time, there is a crackling / intermittent sound. I believe that this is just a problem with the Android system, not with my application.

Starting from API level 9 (gingerbread box), I can apply the session identifier to the audio track, which, I believe, would allow me to play it using SoundPool, which should make playing multiple AudioTracks at the same time smoother.

Right now, due to the number of users still on 2.2, and the fact that I don’t have a gingerbread device, I put off this project until the end.

+1
source

I believe AsyncTask will be the best for you.

0
source

Create an AsyncTask for each AudioTrack and execute them using Excecutor (on my Android 4.4, only the first task will be performed if Excecutor is not used). The following is an example of streaming a microphone signal into AudioTrack and streaming audio files to another AudioTrack in parallel.

 /** Task for streaming file recording to speaker. */ private class PlayerTask extends AsyncTask<Null, Null, Null> { private short[] buffer; private int bufferSize; private final AudioTrack audioTrack; ... @Override protected Null doInBackground(final Null... params) { ... while (!isCancelled()) { audioTrack.write(buffer, 0, bufferSize); } ... } } /** Task for streaming microphone to speaker. */ private class MicTask extends AsyncTask<Null, Null, Null> { private short[] buffer; private int bufferSize; private final AudioTrack audioTrack; ... @Override protected Null doInBackground(final Null... params) { ... while (!isCancelled()) { audioTrack.write(buffer, 0, bufferSize); } ... } } /** On the UI-thread starting both tasks in parallel to hear both sources on the speaker. **/ PlayerTask playerTask = new PlayerTask(); playerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Null()); MicTask micTask = new MicTask(); micTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Null()); 
0
source

All Articles