views:

1020

answers:

5

I am embedding an mp3 into my Flex project for use as a sound effect, but I am finding that every time I play it, there is a delay of about half a second from when I call .play() to when you can hear the sound. This makes it weird because I want the sound effects to sync to game events. My mp3 itself is only about a fifth of a second long so it isn't because of the contents of the mp3.

I'm embedding with

[Embed(source="assets/Tock.mp3")]
[Bindable]
public static var TockSound:Class;
public var tock_sound:SoundAsset;

and then playing with

if (tock_sound == null) {
  tock_sound = new TockSound() as SoundAsset;
}
Alert.show("tock");
tock_sound.play();

I know there's a delay because the sound plays about a half second after the Alert displays. I did consider that maybe it was the initial loading time of constructing the TockSound, but the delay is there on all the subsequent calls as well.

How can I avoid this delay on playing a sound?

Update: It turns out this delay is only present when playing the swf on Linux. I believe it is a Linux-specific flaw in Adobe's flash player.

A: 

I haven't worked with audio in Flash too much but it sounds like the half second delay might be the Flash Player opening up the file and reading it into memory. You could try doing a play() and a stop() when you load the application. That might push it into memory.

The other option is using the StandingWave library which was built by the guys at Noteflight. You can get some additional control over the audio files with that library and hopefully it'll help your delay problem.

Ben Johnson
It happens on all the subsequent times I play the file, too. Also, there isn't really a file. It's embedded into the swf.
lacker
A: 

The problem is that all MP3s have a random amount of blank time at the beginning of the file that is put there during the compression process. Modern software jukeboxes(itunes, songbird etc...) compensate for this by scanning the file before its played and determining the songs actual starting point. Your best bet for sound effects is to use .wav files as their format allows for instant playback, but with a filesize hit.

you might also try: http://www.mptrim.com/ <- they claim to be able to trim the space off the mp3.

greg
This isn't the problem, because when I play the sound 10 times repeated, there is no blank space in the subsequent repeats. Also, Flex doesn't allow embedding of wav files.
lacker
+1  A: 

Not sure about the reason, other than Flash always has had some bad audio latency issues. Read Tinic's blog to stay on top of this stuff: http://www.kaourantin.net/

One thing that might help: make sure your MP3 is 44.1kHz or else Flash will need to resample it.

You can actually embed a WAV file, it just takes work. You embed it as a byte array, and in FP9, dynamically construct a SWF file on the fly. Pretty horrible, but doable. :-) In FP10, you can use the dynamic sound API, so it's easy.

Jolly Roger
+1  A: 

Have you checked http://stackoverflow.com/questions/227674/latency-in-playing-short-sounds-in-flash-9-actionscript-3 for additional help?

zaf
+1  A: 

Try StandingWave

http://code.google.com/p/standingwave/

It has the ability to "cache" the sound before playing getting rid of those delays and clicks you normally hear

Oliver