开发者

AS3 - Sound extract to bytearray - progress handler

I'm using the following to extract the sound data from a sound object and store this in a byte array.

I require that the entire mp3 be loaded into the bytearray before advancing and the below works fine for this purpose however flash temporarily hangs while it extracts this data (2.4mb mp3)

Is there a way i can stop it from hanging i.e. use an eventlistener to check the progress of the extract process?

iniSound();

function iniSound()
{
    playLoadedSound(mp3);
}

function playLoadedSoun开发者_开发技巧d(s:Sound):void
{
    var bytes:ByteArray = new ByteArray();
    s.extract(bytes, int(s.length * 44.1));
    playSound(bytes);
}

function playSound(bytes:ByteArray)
{
    //stop();
    dynamicSound = new Sound();
    dynamicSound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);

    loadedMP3Samples = bytes;
    numSamples = bytes.length / 8;

    phase = 0;
}


This way usually worked on mine

function playLoadedSound (s:Sound) : void
{
    var bytes:ByteArray = new ByteArray ();
    s.extract (bytes, int (s.length * 44.1));
    playSound (s);
}

function playSound (bytes:ByteArray) : void
{
    loadedMp3Samples = bytes;
    loadedMp3Samples.position = 0; // reset position

    dynamicSound = new Sound ();
    dynamicSound.addEventListener (SampleDataEvent.SAMPLE_DATA, onSampleData);
    dynamicSound.play ();
}

function onSampleData (event:SampleDataEvent) : void
{
    for (var i:int ; i < 8192 && loadedMp3Samples.available > 0 ; i++)
    {
        var left:Number = loadedMp3Samples.readFloat ();
        var right:Number = loadedMp3Samples.readFloat ();
        event.data.writeFloat (left);
        event.data.writeFloat (right);
    }
}


The second parameter of extract specifies how much sound you want to extract. Make it smaller and call it every step for as long as you need to completely extract it.


The extract method is run all at once, the potential for monitoring the progress doesn't exist. A better option is to check the 'isBuffering' property of the Sound, and when it's ready start extracting 4096 samples at a time and keep it rolling.. effectively streaming the audio from one Sound object to another.

adding the 'while' statement just breaks up the process into many smaller chunks, the compilier is still trying to do it all within one cycle/frame/whatever.

what are you trying to communicate with a preloader? play head time?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜