我可以像这样播放提示音:

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毫秒短促的哔哔声?

最佳答案

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

您要播放的采样数是采样频率(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 );
}

关于flash - 如何在Actionscript中发出短促的提示音?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14765986/

10-16 02:42