SoundPool play()... better in a thread or in main UI thread?

773 views Asked by At

I noticed that when playing a SFX with soundpool in a thread, it generates a FPS slowdown in the game and when playing the SFX in main thread the game doesn't slow down.

Why soundpool works better playing from mainthread?

This is the play call:

streamID[sound] = soundPool.play(soundID[sound], getSoundEffectsVolume(), getSoundEffectsVolume(), 0, loop, 1);
1

There are 1 answers

0
Oleksandr Albul On

It is because SoundPool is a darkhorse, it is implemented in native layer. So every time you have call SoundPool.play() you will create new thread internally.
So when you call play right in the UI thread, you will get: 1 main thread + 1 AudioTrack thread (that is created inside play call). When you call play in the worker thread you will get: 1 main + 1 worker + 1 play thread. More threads == less resources left.

Also when you call play in ui thread you will get lower latency. Because there will be only one switch: from ui to audioTrack thread, instead of two: from main to worker, from worker to audiotrack thread.