【发布时间】:2011-01-20 00:22:17
【问题描述】:
我正在尝试在播放 mp3 时更新播放器栏。该栏应该代表歌曲的长度,并更新以显示到目前为止歌曲的播放量。
有点丢失 bytesLoaded bytesTotal 还是?
假设栏的宽度为 200px;
我试过把这个和那个分开。没有运气
【问题讨论】:
标签: actionscript-3 actionscript actionscript-2
我正在尝试在播放 mp3 时更新播放器栏。该栏应该代表歌曲的长度,并更新以显示到目前为止歌曲的播放量。
有点丢失 bytesLoaded bytesTotal 还是?
假设栏的宽度为 200px;
我试过把这个和那个分开。没有运气
【问题讨论】:
标签: actionscript-3 actionscript actionscript-2
给你。
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 是以毫秒为单位的声音长度。
【讨论】:
ENTER_FRAME 事件检查进度,这似乎是合理的,因为他之前曾处理过加载程序进度。我添加了一些 cmets 来帮助缩小差距,但目前我不知道我还能走多远。