I'm creating a basic MP3 player in ActionScript 3. I have a basic progress bar that indicates how much of the song has played. The progress is calculated as a decimal percentage normalized between 0 and 1 as such:
var progress:Number = channel.position / sound.length;
The problem is, if the audio is still loading/buffering the sound.length is incorrect. This causes my progress bar to skip around and even travel backwards until the sound has completely loaded and the sound.length isn't changing anymore.
What is the best way to determine the final length of a sound object that is still loading?
Pygame can load WAV, MP3, or OGG files. The difference between these audio file formats is explained at http://invpy.com/formats. To play this sound, call the Sound object's play() method. If you want to immediately stop the Sound object from playing call the stop() method.
mixer pygame module for loading and playing sounds is available and initialized before using it. The mixer module has a limited number of channels for playback of sounds. Usually programs tell pygame to start playing audio and it selects an available channel automatically.
There are at least two options:
1: Leave your progress bar at 0%, and don't move it until the sound has loaded completely. That is:
sound.addEventListener(Event.COMPLETE, onSoundComplete);
private function onSoundComplete(event:Event):void {
// Calculate progress
}
2: Approximate percentage based on the percentage of the file that has already loaded. Something like this:
private var _sound:Sound = /* Your logic here */;
private var _channel:SoundChannel = _sound.play();
_sound.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
private function onSoundProgress(event:ProgressEvent):void {
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
var approxProgress:Number
= _channel.position / _sound.length * percentLoaded;
// Update your progress bar based on approxProgress
}
You can also find out the length of the sound via its ID3 tag if the sound has been properly tagged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With