Ultrashock Forums > Flash > Flash Newbie
help with sound.length

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
help with sound.length
Old 2009-06-22

Hello all,
I'm trying to work with sound for the first time and I've ran into this problem; When ever I try to call the sound.length property I get a 0 returned. Calling it as nameOfMySound.length, anyone run into this before?
postbit arrow 7 comments | 321 views postbit arrow Reply: with Quote   
Registered User
danwoods is offline
seperator
Posts: 35
2009-05-12
danwoods lives in United States
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-06-22 #2 Old  
How soon after you start the sound playing are you trying to get the sound's length? If you are playing an external MP3 file or something then you won't get the full length of the sound until it has fully loaded.

I have a solution for this in my blog:
www.si-robertson.com/actionscript-tips/#total-time-of-streaming-music
Reply With Quote  
danwoods danwoods is offline danwoods lives in United States 2009-06-23 #3 Old  
Can you post another link? I checked it out yesterday and when I got up to implement it today it's been taken down
Reply With Quote  
danwoods danwoods is offline danwoods lives in United States 2009-06-23 #4 Old  
never mind, google cached it
Reply With Quote  
danwoods danwoods is offline danwoods lives in United States 2009-06-23 #5 Old  
I'm still getting zero for a Sound.length. Here's my implemntation of your code:

Calling it:
Code:
var music:Sound = new Sound();
	//jsAlert("From Flash: new sound is created");
	music.load(new URLRequest(newTrack));
	//jsAlert("From Flash: music loading");
	sc = music.play();
	sc.addEventListener(Event.SOUND_COMPLETE, loadNextTrack);
	//get song length
	var songLength:Number = getLength( music );
	//and send it to javascript:setHighlight to time the pulses
	ExternalInterface.call("setHighlight", newTrack, index, songLength);
	isPlaying = true;
and the actual function:
Code:
function getLength( snd:Sound ):Number
{
    var length:Number = 0;

    if( snd.bytesLoaded )
    {
        length = snd.length * ( snd.bytesTotal / snd.bytesLoaded );
    }

    return length;
}
The song files maybe longer than normal song files, with most between 6 and 15 minutes, could that have anything to do with it?
Thanks for the assistance...
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-06-23 #6 Old  
You're not waiting for the file to start loading, Sound.load() isn't a synchronous method. When the file does begin loading you can then read the sound length but it will only be a "best guess" until the file has fully loaded, Flash can't [for example] predict the sound length of variable-bit-rate sound files. If you want the actual sound length before the file is loaded, or has fully loaded, then you will need to use a ByteArray, and assuming the file is MP3, read all of the MPEG audio tags to determine the total sound length. An alternative would be to store the sound length in a XML file or something and load it when needed.

This will be my last post for a while so I wish you all the best with your project.
Reply With Quote  
danwoods danwoods is offline danwoods lives in United States 2009-06-23 #7 Old  
Thanks man, I'll work it out
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-07-03 #8 Old  
Did you manage to get things working?
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: