Delayed playback of short sounds in Flash 9, ActionScript 3

I have some very short sound clips (less than a second) that will play on different events (hover, click, etc.). However, there is usually a significant lag between action and actual sound reproduction. I tried to embed the sound in .swf and load it from the outside at the beginning, but both of them produce the same results. Similarly, I tried compressed and uncompressed sound.

The sound buffers seem to be much longer than I need, as perhaps Flash is optimized more to play longer sounds without any stuttering due to a slightly longer delay when starting sounds. Could this be so? Is there any way to change them? Since what I'm working on will never need to play sounds for more than a second or so long and will always load completely from the very beginning, this will not hurt him to have really short buffers at all.

Another possible thing that could be the reason: if I use WAV files while using loadSound () ... I can't get it to actually play sounds. There are no errors, and everything returns as it should, but no real sound is reproduced, so I have them as .mp3 now. Perhaps when using .mp3 audio (or any compressed audio), there will simply be a lag in decoding it? The reason I still doubt it is that when embedding them in .swf as .wav files (when importing them into the library) they still have the same delay in playback.

Just to test the health, I will include the code that I have, minus the irrelevant parts and error checking. First load them at runtime:

var soundArray:Array = new Array(); loadSound( "click", "sounds/buttondroop4.mp3" ); loadSound( "hover", "sounds/Dink-Public_D-146.mp3" ); function loadSound( name:String, url:String ):void { var req:URLRequest = new URLRequest( url ); soundArray[ name ] = new Sound( req ); soundArray[ name ].addEventListener( Event.COMPLETE, soundLoaded ); } function soundLoaded( event:Event ):void { for( var name:String in soundArray ) { if( event.target == soundArray[name] ) { trace( "Loaded sound [" + name + "]" ); return; } } } function playSound( name:String ):void { for( var nameSrc:String in soundArray ) { if( name == nameSrc ) { var channel:SoundChannel = soundArray[ name ].play(); return; } } } // Sometime later, well after soundLoaded() callback is triggered... playSound( "click" ); playSound( "hover" ); 

And an alternative way, introducing them into the library as classes and from there:

 var sClick:soundClick = new soundClick(); var sHover:soundHover = new soundHover(); sClick.play(); sHover.play(); 

The sound files are tiny, less than 10 kb in total. The disadvantage is obvious enough that one of the first complaints that ever looked at him was that the sound effects on the buttons hung, seemed to linger, so not only was I picky. I feel that I just have to do something wrong; there are too many flash things that have instant sound effects without any close to this lag.

edit: In response to the first answer about the sound files themselves, I already checked, and the sound starts right at the beginning of the file (even cutting off everything except the very first millisecond of sound, I still hear the beginning of the β€œtick” noise that it makes).

+6
flash actionscript-3 audio
source share
6 answers

This is a little, but:

 function playSound( name:String ):void { for( var nameSrc:String in soundArray ) { if( name == nameSrc ) { var channel:SoundChannel = soundArray[ name ].play(); return; } } } 

Must be:

 function playSound(name:String):void { if(soundArray[name]) { soundArray[name].play(); } } 

There is no need to search for a loop, since a hash table is used for this. Also, you shouldn't use Array at all for this, since Array is an ordered set that is indexed using integers. You want to use Object (or Dictionary) in this case and name it soundMap (since it maps sound names to sound objects).

As for the sound delay - they should not be. I made quite a bit of sound in Flash (including tons of one of the rollover and scroll sounds), and that was never a problem. However, Flash Player 10 has a new low-level sound API , which is described by one of the Adobe engineers in this article. The solution associated with this is a bit of a sledgehammer, but maybe you are looking for accuracy in milliseconds.

Fenomas advice gives wisely: check the mp3 file for deadspace at the beginning and end and trim it as close as possible. Also - what is the path from the event handler to your replay request? Are there any blocks there? What is the mp3 format? Flash works best with specific encodings (44.1 Hz and 128 bits, I think).

+6
source share

Well, my first reddening answer is that when I did such things, as a rule, I think that sound files have a certain amount of built-in hours. You can check the sound editor, but what I have done in the past is to import the sound into the Flash IDE, make an empty clip, and put the sound in frame 1 of the clip. Then, but editing the sound of the frame, you get a nice little interface for dragging the start / end points of sound reproduction. Then I used to either attach / remove the clip to play the sound, or leave it somewhere and use the frame commands.

If you are already sure that the runtime is in flash memory and not on audio, then I have no hints or tricks, except for reasonably obvious things, such as playing the sounds of clicking the button down, not up ...

+4
source share

I had the same problem ... until I noticed that the problem only occurs when previewing in Flash. Try running the executable swf (it worked for me).

+4
source share

I have the same problem. Came through it. Apparently, if you keep the sound player "silently" in the background, it does not need to be "restarted" for small sounds. I have not tried it yet ...

http://www.ghostwire.com/blog/archives/as3-fixing-the-lag-that-arises-when-playing-a-short-sound-effect/

+3
source share

One solution could be to use an ActionScript library that reads audio files as binary data.

Standwave
https://github.com/maxl0rd/standingwave3
http://maxl0rd.github.com/standingwave3/

Also, be sure to check your surround sound settings around your SWF. I debugged my application for several hours until I realized that latency came from my connection to bluetooth headphones.

+2
source share

I had a bad sound lunch (about 1 second) with Flash Player 10.0.2.x. There was too much when the sound channel stopped.

I just upgraded to 10.0.22.x and the problem went away.

+1
source share

All Articles