0
votes

I'm having a lot of trouble getting the total sample length of an mp3.

Also, when I load in the mp3 it does not play all the way through. It plays about half of it's length.

The mp3 I'm testing can be found here, http://bit.ly/GYSOYj.

The code I'm using to play the sound is:

[Embed(source='assets/ffmangun_loop_79393.mp3')]
private var _snd_class:Class;
private var sound:Sound = new _snd_class as Sound;


protected function application1_creationCompleteHandler(event:FlexEvent):void {

    sound.length; // 600;
    sound.bytesTotal;// 44652

    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}

Update:
It was suggested that I load the MP3 instead of embed it. The result is the MP3 plays the full length and the length and the bytes total are now different numbers.

// embedded values
bytes total = 44652
sound length = 600.8163265306122

// load dynamic values
bytes total = 44678
sound length = 1776.3265306122448

Update
This needs to be determined at runtime.

1
You seem to be overwriting the same variable 'sampleDuration', unless that's allowed in the future doc? - Neil
Does this also happen if you do not embed but load the file? - TheSHEEEP
@Neil - that's a typo. I've removed that. - 1.21 gigawatts
@TheSHEEEP - it does seem to matter. I've added the details above. - 1.21 gigawatts

1 Answers

3
votes

Firstly, "total samples" != sound.bytesLoaded != sound.length. They are all quite different.

In the code below I've included some other Event listeners to try to illustrated the differences. Listening for 'Event.SOUND_COMPLETE' won't tell you much, first off because it'll only fire when the sound has stopped playing, and secondly because it's been proven to be very unreliable. Using Event.OPEN also doesn't tell you much, as the file hasn't started loading yet, and no info can be retrieved or calculated.

You can either use a ProgressEvent or an EnterFrame event to monitor the loading of the Sound and read the length property, but to determine the total samples you'll also need to know what sample rate was used to create the MP3 and whether it's stereo or mono. Once you know these things, you can easily get the sample length of the file by multiplying the length by the sample rate.

Hopefully this usage example helps -

import flash.net.URLRequest;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;

var sound:Sound;
var soundChannel:SoundChannel;
var sampleRate:Number = 44.1;
var totalSamples:int;

init();
function init( ):void  {
    sound = new Sound();
    sound.load( new URLRequest( "http://bit.ly/GYSOYj") );
    sound.addEventListener( Event.OPEN, soundOpen );
    sound.addEventListener( Event.COMPLETE, soundComplete );

    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, scComplete);
}
function soundOpen( e:Event ):void{
    trace( "OPEN:", sound.length );
}

function soundComplete( e:Event ):void{
    trace( "COMPLETE:", sound.length );

    totalSamples = sound.length * sampleRate;
    trace( "SAMPLES", totalSamples);
}

function scComplete( e:Event):void{
    trace( "sound is complete" );
}

edit: as suggested, one could use as3swf ( bit.ly/HdhVcW ) - but only you can decide if it's worth it or not. Can't imagine what you are trying to build that would require all this..