I’m building a very simple mp3 player. What I need is to make it start playing the music on load, as soon as the page has loaded. Any ideas how should I do that?
This is my code so far. I have this function called musicPlay, I wish to let that function execute on page load.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
public class mp3Player extends MovieClip {
public function mp3Player(){
btnPlay.addEventListener(MouseEvent.CLICK, musicPlay);
function musicPlay (evt:MouseEvent):void {
var myMusic:Sound = new Sound();
var soundFile:URLRequest = new URLRequest('background.mp3');
myMusic.load(soundFile);
myMusic.play();
}
}
}
}
The concept of pages doesn’t really work well in an application, and Flex is primarily used for applications. However, in this case, I suspect you can achieve the desired functionality using the applicationComplete method of Flex’s Application tag.
If you use a generic event as the argument to your musicPlay function, it can respond to both the click and the applicationComplete events.
The syntax you provided us weird, because you normally wouldn’t define a function inside another function. ( Will that even compile? )
Update, since the poster is not using the Flex Framework, the applicationComplete event won’t exist. As such, I’d probably look into putting the mp3 player functionality on a single frame, and doing something on an enterFrame event.