I'm working on a Flash MP3 Player using Actionscript 3. I have a dynamic textbox with an instance name of 'timerText' on the stage next to the buttons for play, pause etc.
The code for updating the timerText field with the current minutes and seconds is called from a function called onEnterFrame, which is attached to the EnterFrame event listener.
I have embedded the fonts in the Dynamic Textbox, and when I call the trace function to print out the current text from the Dynamic textbox, it prints out the time as it should. The textbox itself, however, doesn't change at all.
There is 1 frame in my timeline, with the following actions attached:
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);
}