Playing sounds takes a loooong time?

Playing sounds seems to be very slow on the iPhone - anyone else confirm this?

What I’m seeing is a 220ms average time to kick off a sound using code such as:

   audioSource.clip = clip;
   audioSource.Play();

or even:

audioSource.PlayOneShot( clip, volume );

The 220ms is just time taken to start the clip, i.e. complete the Play call. This is true on the first and subsequent calls for that clip (its not just a one-time thing). The clip is sampled at 44.1kHz for 1.7s duration, about 70kB on disk.

So, its very disruptive to frame rate, especially in the worst case when I have 3-4 sounds playing per second. That’s CPU time spent almost entirely playing sounds!

Is there a way around the slow playback, or a way to improve the slowness? Halp!

I assume thats a WAV right, because mp3 / aac can’t be used or better shouldn’t be used for that due to the 1 track at a time restriction and the time required to startup them on the dedicated decompressor chip

Hmmm, actually its an MP4, do WAVs work better? I could try another format, haven’t done that yet.

they need to be uncompressed, otherwise they are handled by the dedicated decompressor and thats bound to a lag but reduces the cpu usage

That was it, thank you very much dreamora :slight_smile:

For anyone interested in the speedup, I’m seeing 5ms now per clip. Much improved!

HamulTech had a good suggestion on another thread - sometime before the game starts, play each of the sounds for a very short time and then stop them. They should now be cached, and subsequent plays will be faster.

As far as I can tell this is untested, and also relies on something you have no control over (i.e. memory cache, GC). But it might work :slight_smile:

Hmm… it would be nice to have audio clips compressed, even if only to save disk footprint.

I couldn’t find the thread but here’s what I tried so far (and no luck):

Add a GO to the scene, attach the AudioSource component, then get it to reference the clip, and make sure PlayOnAwake is checked. On startup, the clip is played.

But when I use it, I’m still seeing 220ms for play time. Am I doing it right?

Its odd though, if I import the file as a compressed file, it has its DecompressOnLoad checked, which I would think would decompress it and keep it around. That doesn’t seem to be the case, even when I have another reference sitting around in my GO.

scratches head

iPhone doesn’t work that way, it’s too slow…decompress on load has no effect.

–Eric

So you use uncompressed as well, Eric5h5?

Except for music, yes. That’s really the only option. You don’t think anything of playing compressed sounds on a modern computer, because they are really fast, but iPhones are not.

–Eric

I have a few short loops playing (1-2 seconds) of uncompressed Wavs at 22khz… I find the audio glitches quite often, clicking noticably. I am just about to update version to Unity 1.5 - is the audio handling better?

I should actually. We’ve improved some audio related code in 1.5, though those were mostly bugfixes