# Music on load

**URL:** <https://forum.kirupa.com/t/music-on-load/10262>\
**Category:** flash\
**Created:** [December 28, 2002, 5:37am UTC](https://forum.kirupa.com/t/music-on-load/10262 "2002-12-28T05:37:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quantum](https://avatars.discourse-cdn.com/v4/letter/q/b9e5f3/32.png) [@quantum](https://forum.kirupa.com/u/quantum)\
**Post date:** [December 28, 2002, 5:37am UTC](https://forum.kirupa.com/t/music-on-load/10262/1 "2002-12-28T05:37:27Z")

</div>

How do you make music play as soon as a script is done loading?

For example:  
When a person visits a page, the music plays (like [www.sum41.com](http://www.sum41.com)). Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 5:49am UTC](https://forum.kirupa.com/t/music-on-load/10262/2 "2002-12-28T05:49:20Z")

</div>

mySoundObject.onLoad = function(){ this.start();}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 6:01am UTC](https://forum.kirupa.com/t/music-on-load/10262/3 "2002-12-28T06:01:15Z")

</div>

ok, I’m new here on the AS scripting, but the code didn’t work.

What exactly do I have to type?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 6:10am UTC](https://forum.kirupa.com/t/music-on-load/10262/4 "2002-12-28T06:10:27Z")

</div>

well yes first you would have to define your sound object then load a file into it, then using onLoad you can know when the sound has finished loading and tell it to start, which is what this.start() refers to. ‘this’ represents the sound. So you would have something like

mySoundObject = new Sound();  
mySoundObject.loadSound(“mySong.mp3”)  
mySoundObject.onLoad = function(){ this.start();}

when mySong.mp3 is loaded (make sure its placed in the same directory as the swf) then it plays

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 6:15am UTC](https://forum.kirupa.com/t/music-on-load/10262/5 "2002-12-28T06:15:39Z")

</div>

Thanks, but the didn’t work to well. What happened was that the song will not play. It loads ok, but won’t play.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 3:02pm UTC](https://forum.kirupa.com/t/music-on-load/10262/6 "2002-12-28T15:02:41Z")

</div>

my mistake, simple error in order of operations:

mySoundObject = new Sound();  
mySoundObject.onLoad = function(){ this.start();}  
mySoundObject.loadSound(“mySong.mp3”);

sorry 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 28, 2002, 4:05pm UTC](https://forum.kirupa.com/t/music-on-load/10262/7 "2002-12-28T16:05:29Z")

</div>

Ok, thank you. Is there a way to make it not loop over a previously playing one (having the same song play twice together)?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 29, 2002, 4:55pm UTC](https://forum.kirupa.com/t/music-on-load/10262/8 "2002-12-29T16:55:53Z")

</div>

you can tell it to stop before telling it to start, so if it is playing already, it will stop itself and then start a new
