Adobe Air & Adobe Flex & ActionScript & Mobile Dev & HTML5 & RIA & User Experience
flash.media.sound 在Flash Player version 10 及更高版本中,可以處理動態生成的聲音。這種情況下,Sound 對象將使用向 flash.events.SampleDataEvent 事件處理函數分配的函數來輪詢聲音數據。從用聲音數據填充的 ByteArray 對象檢索聲音的同時播放聲音。可以使用 Sound.extract() 從 Sound 對像中提取聲音數據,然後在將聲音數據寫回到流以進行播放之前可以對其進行操作。
因此按照例子寫了2種不同版本的改變聲音速度的例子,2個例子區別在于
SampleDataEvent 處理方式得不同。
一個是改變了音速(帶有“piapia”聲)
改變了音速(帶有“piapia”聲)的SampleDataEvent 處理方式
/** * mp3Real.SampleDataEvent.SAMPLE_DATA **/ private function onSampleData(e:SampleDataEvent):void { currentPosition = uint(_position); var currentTimeIntDiff_1:uint = uint(_position) / 44100; currentTime = toDoubleString(uint(currentTimeIntDiff_1 / 60)) + ":" + toDoubleString(currentTimeIntDiff_1 % 60); var bytes:ByteArray = new ByteArray(); mp3Proxy.extract(bytes, 4096, _position); bytes.position = 0; e.data.writeBytes(bytes); if(!isThumbPress) hs_time.value = _position / maxPosition; bytesSpeed = playbackSpeed * BYTES_PER_CALLBACK; _position += bytesSpeed; }
改變了音頻(像以前的磁帶)的SampleDataEvent 處理方式
/** * mp3Real.SampleDataEvent.SAMPLE_DATA **/ private function onSampleData(e:SampleDataEvent):void { var l:Number; var r:Number; var p:uint; var loadedSamples:ByteArray = new ByteArray(); currentPosition = uint(_phase); bytesSpeed = playbackSpeed * BYTES_PER_CALLBACK; var currentTimeIntDiff_2:uint = currentPosition / 44100; currentTime = toDoubleString(uint(currentTimeIntDiff_2 / 60)) + ":" + toDoubleString(currentTimeIntDiff_2 % 60); if (_isPlayingSound && currentTime == maxTime) { _isPlayingSound = false; } if(!isThumbPress) hs_time.value = _phase / maxPosition; mp3Proxy.extract(loadedSamples, BYTES_PER_CALLBACK * playbackSpeed, currentPosition); loadedSamples.position = 0; while (loadedSamples.bytesAvailable > 0) { p = uint(_phase - currentPosition) * 8; if (p < loadedSamples.length - 8 && e.data.length <= BYTES_PER_CALLBACK * 8) { loadedSamples.position = p; l = loadedSamples.readFloat(); r = loadedSamples.readFloat(); e.data.writeFloat(l); e.data.writeFloat(r); } else { loadedSamples.position = loadedSamples.length; } _phase += playbackSpeed; } }
不知道各位有沒有既能改變速度又不會調高音頻的辦法,請賜教。
Related posts:
moinca
四月 27th, 2010 at 10:01 上午
Good Post!