dg85
dg85

Reputation: 490

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 playLoadedSound(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;
}

Upvotes: 0

Views: 5331

Answers (3)

Januartha
Januartha

Reputation: 139

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);
    }
}

Upvotes: 1

NickHubben
NickHubben

Reputation: 81

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?

Upvotes: 0

Maurycy
Maurycy

Reputation: 1322

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.

Upvotes: 0

Related Questions