Flash 如何在Actionscript中播放短嘟嘟声?
我可以播放如下的哔哔声:Flash 如何在Actionscript中播放短嘟嘟声?,flash,audio,actionscript,beep,Flash,Audio,Actionscript,Beep,我可以播放如下的哔哔声: 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++) {
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);
}
private-var-beep:Sound=new-Sound();
私有函数beepInit():void{
var beepHandler:Function=new Function();
beepHandler=函数(事件:SampleDataEvent):无效{
对于(变量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_数据,beepHandler);
}
在应用程序启动时,我调用beepInit()
要播放,请调用:beep.play()
这是连续的声音。如何将其设置为500毫秒短蜂鸣声?一旦达到要播放的长度,您需要停止创建样本。您可以通过检查创建的样本量与要播放的样本量来完成此操作 要播放的采样量是采样频率(44100/秒)乘以要播放的声音长度(以秒为单位)
private const采样频率:uint=44100;
创建的私有变量样本:uint=0;
私有变量长度秒:数字=0.5;
private-var-beep:Sound=new-Sound();
私有函数beepInit():void{
变量beepHandler:Function=Function(事件:SampleDataEvent):void{
对于(变量i:uint=0;i<2048;i++){
if(创建的样本>=样本频率*长度秒){
返回;
}
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_数据,beepHandler);
}
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 );
}