Android SoundPool stutters, lags, sometimes does not play at all

Use case

I have 68 piano samples, each approx. 174 kB, Ogg data, Vorbis sound, stereo, s16, 44100 Hz, 352 kbit / s, 4 seconds.

I need to play different combinations of them at the same time (musical term: harmonic chords / intervals), for example. 1st + 7th , 14th + 22nd , etc.

Also: the current โ€œplayโ€ needs an option to stop it after t milliseconds.

What i tried

The obvious solution is to use SoundPool . After setting maxStreams = 20 , I .load() all the samples to it. Each playback request creates a stream (valid: Scala future ), which waits for the specified t millis , and then stops all sounds.

It seems to work ... usually.

Problem

  • This SoundPool , while playing 2 (or more) sounds at the same time, sometimes does not start them exactly at the same time, of course, you can hear a slight difference in their start time. However, most of the time this is normal.

  • There is also a problem with stopping ... sometimes the pair "lasts" longer, but then again, usually it ends at the time I want it.

  • Finally, which is really strange: each .play() , which is the first after:

    • off (or even dimmed only!) the display or
    • application launch ...

    ... will not play sound !

Question

Does Android support a more reliable (in terms of synchronization consistency) implementation of the audio channel pool?

Maybe itโ€™s better to work if I created 68 threads, one for each sound? ... I donโ€™t think so.

+4
source share
1 answer

Edit: possible solution (isn't it so elegant?)

Tiago Rosa writes that he overcame SoundPool lag

(...) playing a muffled sound in a loop (...)

After several tests, I noticed that sometimes the โ€œplayโ€ function performed 8 ms, and sometimes it was instantaneous.

Conclusion, it seems that when nothing is playing, SoundPool is reset, and when it is playing again, it takes time to initialize.

But I'm not sure. Is it effective? How about the battery?

My tests show it is definitely worth it. Samples are reproduced sequentially, in just a few hundred tests, only 3-4 were few . Two other problems (not playing at all and inconsistent stopping times) also disappeared!

However, the battery problem remains open ...

+3
source

Source: https://habr.com/ru/post/1488039/


All Articles