Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Getting the length of audio file

AS3 audio length

2:33 pm on Mar 31, 2009 (gmt 0)

New User

10+ Year Member

joined:Mar 8, 2006
posts: 30
votes: 0

How do I get the length of an audio file? I need this value to set up a timer that depends on the length of the song playing.

I have tried the following, but only get a value of ZERO. The audio plays just fine. Please advice!

from my code:

var gettheme:URLRequest;
var nextA: Sound;
var SC_A:SoundChannel;
var songLong : Number = 5; // just to set a number

gettheme = "mysong.mp3"
nextA = new Sound(gettheme);
SC_A = nextA.play();
songLong = nextA.length;
trace("Song length is: ", songLong );

If I try songLong = nextA.length(); // ie, .length();
I get an error:
1195: Attempted access of inaccessible method length through a reference with static type flash.media:Sound.

Please advice!


8:05 pm on Mar 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

I think you might have to do a bytesLoaded/bytesTotal check as you won't know the length until it's completely loaded. When bytesLoaded=bytesTotal, then you might be able to query for length, which will give you length in milliseconds. With .flv files, this is inherent in the file (presuming in the file headers) so you can get a video length as soon as it loads, but appears to be different with the Sound() object.

Dig around the Sound() class in the AS3 manual of the help files.