早上好!
我在试着为MP3创建一个可视波形。我所包含的代码在成功加载MP3时被调用。我打算从声音中提取一些重要的样本来创建波形,而不是将整个声音提取到一个字节数组中。即使在好的机器上,提取整首歌曲也会导致flash冻结3-5秒(或更长时间!)。就我而言,这是不可行的。
不幸的是,我下面的代码不能产生任何数字。如果我提取整首歌,它会起作用,但只提取关键点不会给我任何东西。执行提取是否会使声音对象的其余部分对于将来的提取无效?如果是这样,有没有办法在提取过程中不会长时间冻结闪光灯?
代码其余部分中的一些重要变量:
waveFormWidth:波形精灵的静态宽度。
waveFormHeight:波形精灵的静态高度。
song:我将用来创建波形的声音对象。
public function mapWaveForm(e:Event = null):void
{
// Clear the wave form sprite
waveForm.graphics.clear();
waveForm.graphics.lineStyle(0, 0x000000, 1);
// Storage for the wave form bit data
var byteOutput:ByteArray;
var leftPeakSize:Number;
var rightPeakSize:Number;
var songTotalSamples:int;
var thisSample:int;
byteOutput = new ByteArray();
// How many samples?
songTotalSamples = (song.length * 44.1);
// Loop for the wave form width
for (var peakCount:int = 0; (peakCount < waveFormWidth); peakCount++)
{
// Get info at each peak.
thisSample = Math.floor(songTotalSamples * (peakCount / waveFormWidth));
song.extract(byteOutput, 1, thisSample);
byteOutput.position = 0;
trace(thisSample, byteOutput.readFloat());
leftPeakSize = byteOutput.readFloat() / 1.27;
rightPeakSize = byteOutput.readFloat() / 1.27;
// Turn those peaks into something usable.
leftPeakSize = leftPeakSize * (waveFormHeight * .5);
rightPeakSize = rightPeakSize * (waveFormHeight * .5);
// Make the left channel line
waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) - leftPeakSize);
// Make the right channel line
waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) + rightPeakSize);
}
}谢谢你们的帮助!
发布于 2012-06-26 00:52:35
不管它有什么价值,我只是在做完全相同的事情,我只是在一些研究之后才让它工作。我似乎不能让它与WAV文件工作,但这是兼容所有类型的MP3文件。这就是我最终得到的结论:
var IMAGE_SIZE:int = new int(600); // Final width of image
var IMAGE_DEPTH:int = new int(5); // How many passes per pixel of image
var RESOLUTION:int = new int(IMAGE_SIZE * IMAGE_DEPTH);
var DRAW_AMPLITUDE:int = new int(150); // pixel height of max volume line
var waveForm:Shape = new Shape();
var mp3File:Sound = new Sound();
mp3File.load(new URLRequest("audio.mp3"));
mp3File.addEventListener(Event.COMPLETE, parseSound);
function parseSound(e:Event):void {
var soundBytes:ByteArray = new ByteArray();
for (var i:int=0; i<RESOLUTION; i++) {
mp3File.extract(soundBytes, 1, Math.floor((mp3File.length*44.1)*(i/RESOLUTION)));
soundBytes.position = 0;
while (soundBytes.bytesAvailable > 0) {
var tmpNum:Number = new Number();
tmpNum += soundBytes.readFloat();
}
drawWave(i, tmpNum);
soundBytes.clear();
}
this.addChild(waveForm);
trace("--DONE--");
}
function drawWave(i:Number, amp:Number):void {
var pixX:Number = new Number(Math.floor(i/IMAGE_DEPTH));
var pixY:Number = new Number((amp*DRAW_AMPLITUDE)/2);
waveForm.graphics.lineStyle(1, 0x0, (1.2/IMAGE_DEPTH));
waveForm.graphics.moveTo(pixX, ((DRAW_AMPLITUDE/2)-pixY));
waveForm.graphics.lineTo(pixX, ((DRAW_AMPLITUDE/2)+pixY));
}发布于 2012-04-13 15:16:29
这是一个老帖子,也许我没有正确理解你的问题,但是提取方法允许你指定样本的长度和开始的位置。
"public function extract(target:ByteArray, length:Number, startPosition:Number = -1):Number"length是要提取的样本数,startPosition是byteArray中的起始位置。
请参阅http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html#extract%28%29
发布于 2011-05-29 22:36:22
使用这个类:
http://www.bytearray.org/?p=329
我还把这个整合到了一个非常糟糕的,半生不熟的实验中,我很早就开始做一个混音器。你也可以在这里获得它的源代码:
http://code.google.com/p/ascensionsystems/downloads/list
https://stackoverflow.com/questions/6168056
复制相似问题