Вот, пожалуйста.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Engine extends Sprite
{
private var _sound:Sound;
private var _soundBar:Sprite;
private var _soundChannel:SoundChannel;
public function Engine()
{
addSoundChannel();
drawSoundBar();
//load sound
loadSound("sound.mp3");
}
private function addSoundChannel():void
{
_soundChannel = new SoundChannel();
}
private function drawSoundBar():void
{
//draw box 200px x 10px with a red fill
_soundBar = new Sprite();
_soundBar.graphics.beginFill(0xFF0000);
_soundBar.graphics.drawRect(0, 0, 200, 10);
_soundBar.graphics.endFill();
addChild(_soundBar);
_soundBar.x = (stage.stageWidth/2) - (_soundBar.width/2);
_soundBar.y = (stage.stageHeight/2) - (_soundBar.height/2);
}
private function loadSound(url:String):void
{
var toLoad:URLRequest = new URLRequest(url);
_sound = new Sound();
_sound.load(toLoad);
_sound.addEventListener(Event.COMPLETE, soundLoaded, false, 0, true);
}
private function soundLoaded(evt:Event):void
{
trace("loaded");
_soundChannel = _sound.play();
addListeners();
}
private function addListeners():void
{
stage.addEventListener(Event.ENTER_FRAME, checkSound, false, 0, true);
_soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
}
private function checkSound(evt:Event):void
{
//adjust scaleX from 0 to 1 based on sound position
_soundBar.scaleX = _soundChannel.position / _sound.length;
}
private function soundComplete(evt:Event):void
{
trace("done");
stage.removeEventListener(Event.ENTER_FRAME, checkSound);
}
}
}
SoundChannel.position
захватывает текущую позицию в миллисекундах, в то время как Sound.length
- это длина звука в миллисекундах.