2011-04-03 58 views
0

我有下面的代碼加載一個聲音,'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); 

} 

回答

0

我解決了這個問題,而不是在每次迭代中都回過頭來讀取一半字節,而是在每隔一次迭代時重複所有字節。所以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); 
} 
1

您正在運行一個無限循環,你就通過字節數組增加了,然後回來,但再往前所以你做了整整6步向前,向後4。我會在這裏將代碼全部改爲一起擺脫while循環將其替換爲有條件的。我會有一系列的迭代,並確保你在字節陣列中上下的方式不會讓你超出數組的範圍,這可能是這裏發生的事情。如果可能的話,使用數組訪問器(bytearray [index])訪問二進制數據並迭代條件(i = n; i < bytes.length; ++ i)。

+0

感謝您的回答!任何關於如何開始執行for循環的指針?正如你可以告訴的那樣,我對ActionScript的經驗不是很多...... – DLiKS 2011-04-03 20:31:25