I've got 68 piano samples, each approx. 174 kB, Ogg data, Vorbis audio, stereo, s16, 44100 Hz, 352 kb/s, 4 seconds.
I need to simultaneously play different combinations of them (musical term: harmonic chords/intervals), e.g. 1st + 7th
, 14th + 22nd
etc.
Also: current "playback" needs an option to stop it after t
milliseconds.
An obvious solution seems to be to use a SoundPool
. After setting maxStreams = 20
, I .load()
all the samples to it. With every play request a thread (really: Scala's future
) is created that awaits given t millis
and then stops all sounds.
It seems to work... usually.
This SoundPool
, when playing 2 (or more) sounds simultaneously, sometimes won't start them exactly at the same time, slight difference in their start times certainly can be heard. Most of the time, however, it's OK.
There's also a problem with stopping... sometimes a pair "lasts" longer, but again, usually it ends at the time I want it to.
Finally, what's really weird: every .play()
that is a first one after either:
...won't produce a sound!
Does Android provide us with a more reliable (in terms of timing consistency) sound pool implementation to use?
Would it perhaps work better if I created 68 threads, one for each sound?... I don't think so.
Thiago Rosa writes, that he's overcome the lagging of SoundPool
(...) by playing a muted sound in loop (...)
After a few tests, I noticed that sometimes the “play” function took 8ms to execute and sometimes it executed instantly.
Conclusion, it seems that when there is nothing playing, SoundPool is resetting and when it is going to play again, it takes time to initialize.
But I'm not really convinced. Is it efficient? What about the battery?
My tests indicate that it's definitely worth a shot. The samples play consistently now, in a few hundred tests only 3-4 were a little off time. The two other issues (not playing at all and inconsistent stop timing) are gone now, too!
However, battery issue remains open...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With