1

我可以播放这样的哔声:

private var beep:Sound = new Sound();

private function beepInit():void {
    var beepHandler:Function = new Function();

        beepHandler = function(event:SampleDataEvent):void {
            for (var i:uint = 0; i < 2048; i++) {
                var wavePos:Number = 20 * Math.PI * i / 2048;
                event.data.writeFloat(Math.sin(wavePos));
                event.data.writeFloat(Math.sin(wavePos));
            }
        }

        beep.addEventListener(SampleDataEvent.SAMPLE_DATA, beepHandler);
}

在应用程序启动时,我调用 beepInit();

要播放,请调用: beep.play();

这是连续的声音。我怎样才能做到前。500 毫秒。短哔声?

4

1 回答 1

3

一旦达到要播放的长度,您就需要停止创建样本。您可以通过检查创建的样本数量与要播放的样本数量来做到这一点。

您要播放的样本量是样本频率(44100/秒)乘以您要播放的声音长度(以秒为单位)。

private const sampleFrequency:uint = 44100;
private var samplesCreated:uint = 0;
private var lengthInSeconds:Number = 0.5;
private var beep:Sound = new Sound();

private function beepInit():void {
  var beepHandler:Function = function ( event:SampleDataEvent ):void {
    for (var i:uint = 0; i < 2048; i++) {
      if (samplesCreated >= sampleFrequency * lengthInSeconds) {
        return;
      }
      var wavePos:Number = 20 * Math.PI * i / 2048;
      event.data.writeFloat( Math.sin( wavePos ) );
      event.data.writeFloat( Math.sin( wavePos ) );
      samplesCreated++;
    }
  };

  beep.addEventListener( SampleDataEvent.SAMPLE_DATA, beepHandler );
}
于 2013-02-08T08:49:43.897 回答