在 Flash 9、Actionscript 3 中播放短声音的延迟 [英] Latency in playing short sounds in Flash 9, Actionscript 3

查看:17
本文介绍了在 Flash 9、Actionscript 3 中播放短声音的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常短的音频剪辑(不到一秒长)可在各种事件(按钮悬停、点击等)上播放.但是,动作与实际播放声音之间通常存在明显的滞后.我已经尝试将声音嵌入到 .swf 中,并在开始时从外部加载它,但两者都会导致相同的结果.同样,我尝试使用压缩和未压缩的音频.

I've got a few very short audio clips (less than a second long) to be played on various events (button hover, click, etc). However, there is usually a significant lag between the action and the actual playing of the sound. I have tried both embedding the sound in the .swf, and loading it externally at the start, but both lead to the same results. Likewise, I've tried with compressed and uncompressed audio.

看起来是音频缓冲区比我需要的要长很多,比如 Flash 可能更适合播放更长的声音而没有任何口吃启动声音的延迟更多.会是这样吗?有什么办法可以改变它们吗?由于我正在处理的内容永远不需要播放超过一秒或那么长的声音,并且总是在开始时完全加载,因此拥有非常短的缓冲区根本不会伤害它.

What it seems like is that the audio buffers are just a lot longer than I need them to be, like perhaps Flash is optimized more towards playing longer sounds without any stutter at the expense of a little more latency in starting sounds. Could this be it? Is there any way to change them? Since what I'm working on will never need to play sounds more than a second or so long and will always be loaded completely at the start, it wouldn't hurt it at all to have really short buffers.

另一个可能的原因:如果我在使用 loadSound() 时使用 .wav 文件...我无法让它实际播放声音.没有错误,一切都按原样返回,但没有播放实际声音,这就是我目前将它们作为 .mp3 的原因.也许在使用 .mp3 音频(或任何压缩音频)时,解码会出现延迟?不过,我仍然对此表示怀疑的原因是,当将它们作为 .wav 文件嵌入到 .swf 中时(通过将它们导入库中),它们在播放时仍然具有相同的延迟.

One other possible thing that might be the cause: if I use .wav files when using loadSound()... I can't get it to actually play the sounds. There's no errors, and everything returns as it should, but no actual sound is played, which is why I have them as .mp3 currently. Perhaps when using .mp3 audio (or any compressed audio), there just will be lag in decoding it? The reason I still have doubts about this, though, is that when embedding them in the .swf as .wav files (by importing them into the library), they still have the same latency on playback.

只是为了进行完整性检查,我将包含我拥有的代码,减去不相关的部分和错误检查.首先,在运行时加载它们:

Just for a sanity check, I'll include the code I've got, minus irrelevant parts and error checking. First, loading 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 alternate way, embedding them in the library as classes and going from there:

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

声音文件很小,一般小于10kb.延迟很明显,以至于有人看到它时首先抱怨的一个是按钮悬停的声音效果似乎延迟了,所以不仅仅是我挑剔.我觉得我一定是做错了什么;有太多闪光的东西有活泼的声音效果而没有这种滞后.

The sound files are tiny, less than 10kb generally. The lag is apparent enough that one of the first complaints someone had when looking at it was that the sound effects on button hovers seemed delayed, so it wasn't just me being picky. I feel like I must just be doing something wrong; there's too many flash things out there that have snappy sound effects without anywhere near this kind of lag.

响应关于声音文件本身的第一个响应,我已经检查过,声音在文件的开头立即开始(即使剪掉了声音的第一毫秒之外的所有内容,我仍然可以听到它发出的滴答"声的开始).

edit: In response to the first response about sound files themselves, I've already checked, and the sound starts immediately at the start of the file (even clipping out everything but the very first millisecond of sound, I can still hear the start of the 'tick' noise it makes).

推荐答案

这是一件小事,但是:

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

应该是:

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

不需要循环查找,因为这就是哈希表的用途.此外,您根本不应该为此使用 Array,因为 Array 是使用整数索引的有序集.在这种情况下,您想使用一个对象(或字典)并将其命名为 soundMap(因为它将声音名称映射到声音对象).

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

至于声音延迟——应该没有.我已经在 Flash 中制作了相当多的声音(包括大量的一次性翻转和推出声音),这从来都不是问题.但是 Flash Player 10 有一个新的 low-level sound API 由该文章中的一位 Adob​​e 工程师描述.涉及到的解决方案有点像大锤,但也许您正在寻找毫秒精度.

As for sound latency -- there should be none. I've done quite a bit of sound in Flash (including tons of one off rollover and rollout sounds) and it has never been an issue. However Flash Player 10 has a new low-level sound API which is described by one of the Adobe engineers in that article. A solution involving that is a bit of a sledge hammer but perhaps you are looking for millisecond accuracy.

fenomas 给出的建议是明智的:检查 mp3 文件的开头和结尾是否有死区,并尽可能地修剪它.另外 - 从事件处理程序到您的播放语句的路径是什么?那里有可能的块吗?mp3的格式是什么?Flash 最适合特定编码(我相信 44.1 hHz 和 128 位).

The advice fenomas gives is wise: check the mp3 file for deadspace at the start and end and trim it as close as possible. Also - what is the path from the event handler to your play statement? Are there any possible blocks there? What is the format of the mp3? Flash works best with specific encodings (44.1 hHz and 128 bit I believe).

这篇关于在 Flash 9、Actionscript 3 中播放短声音的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆