我正在使用 Actionscript 3 开发 Flash MP3 播放器。我在舞台上的播放、暂停等按钮旁边有一个实例名称为“timerText”的动态文本框。
使用当前分钟和秒更新 timerText 字段的代码是从一个名为 onEnterFrame 的函数调用的,该函数附加到 EnterFrame 事件侦听器。
我已经在动态文本框中嵌入了字体,当我调用跟踪函数从动态文本框中打印出当前文本时,它会打印出应有的时间。然而,文本框本身并没有改变。
我的时间轴中有 1 帧,附有以下操作:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.events.Event;
// Stop the Player
stop();
/**
* Declare some Variables
*/
// Get the MP3 File
var req:URLRequest = new URLRequest('audio/alice-track-1.mp3');
// Create a Sound Object
var snd:Sound = new Sound();
// Create a Sound Channel
var channel:SoundChannel;
// Initialise the Pause Position
var pausePosition:int = 0;
// Boolean for Is Playing(?)
var isPlaying:Boolean = false;
// Set the Play Buffer to 5 Seconds
var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
// Load the Requested URL into the Snd Var, along with the Context
snd.load(req, context);
// Create the Play Channel
channel = snd.play();
// Set IsPlaying to TRUE initially
isPlaying = true;
/**
* Play Music Function
*/
function playSound(event:MouseEvent):void {
if (isPlaying == false) {
channel = snd.play(pausePosition);
isPlaying = true;
}
}
/**
* Stop Music Function
*/
function stopSound(event:MouseEvent):void {
channel.stop();
pausePosition = 0;
isPlaying = false;
}
/**
* Pause Music Function
*/
function pauseSound(event:MouseEvent):void {
pausePosition = channel.position;
channel.stop();
isPlaying = false;
}
// Add the Button Event Listeners
playBtn.addEventListener(MouseEvent.CLICK, playSound);
stopBtn.addEventListener(MouseEvent.CLICK, stopSound);
pauseBtn.addEventListener(MouseEvent.CLICK, pauseSound);
// Add the OnEnterFrame Event Listener
addEventListener(Event.ENTER_FRAME, onEnterFrame);
/**
* Initialisation / OnEnterFrame Function
*/
function onEnterFrame(event:Event):void {
var totalSeconds:Number = channel.position / 1000;
var minutes:Number = Math.floor(totalSeconds / 60);
var seconds = Math.floor(totalSeconds) % 60;
if (seconds < 10) {
seconds = '0' + seconds;
}
timerText.text = (minutes + ':' + seconds);
trace(timerText.text);
}