sound tips

ASaudio&SoundAS 两个开源项目阅读:

ASaudio&SoundAS 都是比较小巧的声音控制,但似乎都不能直接拿到项目只直接使用。

ASaudio

ASaudio的Track,Group和Tween的TweenCore SimpleTimeline有些类似,但相对简单点。

它的音量控制是也是通过enterFrame+getTimer实现的。

但项目中已经有了通过的enterFrame,如果直接使用的话,会有些浪费,需要嵌入到项目中才比较好。

另外ASaudio也是在enterFrame中监听audio是否播放完成,进而抛出事件的。这个避免了原生的sound在播放完成时可能存在的bug。

这个bug是 在播放完一段声音时soundchannel.position 和 sound.length的值有差距。测试结果:soundchannel.position< sound.length

SoundAS

SoundAS相对来说更实用一点。它引用了Signal类库,如果在项目中采用的话,要么也同时引起该类库,要么再做修改。工作量一样很大。

对于同一个url,SoundAS做了缓存处理,不会new一个sound,这点比ASaudio好。

SoundAS的代码注释中还提供的一个channel bug,url地址:http://www.stevensacks.net/2008/08/07/as3-sound-channel-bug/

这个bug时,如果循环播放一段声音,通过channel.position得到的位置可能是超出声音长度,当你使用这个位置数据调用play方法时就会报错。

原文如下:

I don't know if this has been documented or not.

If you set a Sound to loop, it's channel.position property returns a number out of range of the Sound.length after it loops. The position property just keeps on incrementing beyond the length when you loop a sound.

If you try to play a Sound from that out of range position, the Sound glitches out, because that position is invalid, even though Flash returns it to you, implying it's valid. Flash should not return an invalid position to you.

This all revolves around the fact that Sound has absolutely no way to pause right now. I don't know why Adobe did not give us a channel.pause() method. Because they chose not to, this bug has bad consequences.

The only way to pause and unpause a sound in Flash is to store the current position of the channel and call channel.stop(), and when you unpause, you call sound.play(position). This works fine if you're only playing the sound once. If you're looping, however, it doesn't at all.

If the sound is looping, the position returned will eventually be higher than the length, which means the Sound glitches because you're passing an out of range position; a position, once again, that Flash returned to you as valid.

Wait, it gets worse. If you unpause a looping sound by passing its position, when it loops, it will loop from that position, not the beginning of the Sound. Problem!

There is no pause method for channel, and without a pause method, there is no workaround for the above bug and issue except to manually loop a Sound by adding an event listener to the sound complete event (which can be unreliable), calling play on it again and managing the looping manually, which undermines the loops argument of the Sound.play() method and requires a fair amount of code.

Adobe, we need a channel.pause(flag:Boolean) method.

原文地址:https://www.cnblogs.com/tianlanliao/p/3462965.html