7

Use case

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.

What I've tried

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.

The problem

  • 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:

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

    ...won't produce a sound!

Question

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.

4

1 回答 1

4

Edit: possible solution (not so elegant?)

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...

于 2013-06-25T10:34:55.843 回答