ActionScript 3 - RangeError:Error#2004 - 我在做什么错?
问题描述:
我有下面的代码加载一个声音,'test.mp3',然后降低它的音调,也放慢了它。声音播放的音调较低,但在采样结束时,我收到了以下错误:'RangeError:Error#2004:其中一个参数无效'。我做错了什么,我该如何解决这个问题?任何对此的帮助将非常感激。ActionScript 3 - RangeError:Error#2004 - 我在做什么错?
var sourceSound:Sound = new Sound();
var outputSound:Sound = new Sound();
var urlRequest:URLRequest=new URLRequest('test.mp3');
sourceSound.load(urlRequest);
sourceSound.addEventListener(Event.COMPLETE, soundLoaded);
function soundLoaded(event:Event):void {
outputSound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
outputSound.play();
}
function processSound(event:SampleDataEvent):void {
var bytes:ByteArray = new ByteArray();
sourceSound.extract(bytes, 4096);
var returnBytes:ByteArray = new ByteArray();
bytes.position=0;
while (bytes.bytesAvailable > 0) {
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
bytes.position -= 4;
returnBytes.writeFloat(bytes.readFloat());
}
event.data.writeBytes(returnBytes);
}
答
我解决了这个问题,而不是在每次迭代中都回过头来读取一半字节,而是在每隔一次迭代时重复所有字节。所以processSound函数现在看起来是这样的:
function processSound(event:SampleDataEvent):void {
var bytes:ByteArray = new ByteArray();
sourceSound.extract(bytes, 4096);
bytes.position=0;
var returnBytes:ByteArray = new ByteArray();
var count:int;
while (bytes.bytesAvailable > 0) {
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
count++;
if (count%2 === 0) {
bytes.position-=8;
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
}
}
event.data.writeBytes(returnBytes);
}
答
您正在运行一个无限循环,你就通过字节数组增加了,然后回来,但再往前所以你做了整整6步向前,向后4。我会在这里将代码全部改为一起摆脱while循环将其替换为有条件的。我会有一系列的迭代,并确保你在字节阵列中上下的方式不会让你超出数组的范围,这可能是这里发生的事情。如果可能的话,使用数组访问器(bytearray [index])访问二进制数据并迭代条件(i = n; i < bytes.length; ++ i)。
感谢您的回答!任何关于如何开始执行for循环的指针?正如你可以告诉的那样,我对ActionScript的经验不是很多...... – DLiKS 2011-04-03 20:31:25