开发者

Execute script on page load in ActionScript 3.0

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();
            }

        }
    }
}


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?

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.

        public function musicPlay (evt:Event):void {
            var myMusic:Sound = new Sound();
            var soundFile:URLRequest = new URLRequest('background.mp3');
            myMusic.load(soundFile);
            myMusic.play();
        }

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.


Well since the code in your movie will start running as soon as its loaded, which will be around (just before?) page load time, why dont you just call the function that starts the music instead of listening for a mouse event?

If you need to make sure the page is loaded before starting the music for some reason (doubtful), then you would need some javascript in the host page which listens for the onload event etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜