# Stop music playing straight away

**URL:** <https://forum.kirupa.com/t/stop-music-playing-straight-away/299269>\
**Category:** Uncategorized\
**Created:** [November 2, 2009, 1:38pm UTC](https://forum.kirupa.com/t/stop-music-playing-straight-away/299269 "2009-11-02T13:38:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![prycey](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@prycey](https://forum.kirupa.com/u/prycey)\
**Post date:** [November 2, 2009, 1:38pm UTC](https://forum.kirupa.com/t/stop-music-playing-straight-away/299269/1 "2009-11-02T13:38:40Z")

</div>

hello, i am trying to stop the music playing straight away, but instead only when the button is clicked.

any ideas? 🙂

```auto

stop(); 
//Stop at this frame

var music:Sound = new Sound()
//Create a new sound object

var req:URLRequest = new URLRequest("My Song 2.mp3");
//Set location of sound file 

var channel:SoundChannel; 
//Create a new SoundChannel object

music.load(req);
//load music file into channel object

playMusic(); 

var musicIsOn:Boolean = true;
//Boolean checks if the sound is playing

guitarButton.addEventListener(MouseEvent.CLICK, stopMusic); 
//An event listener for a button that stops the music

function stopMusic(evt:Event):void 
{

    if(musicIsOn)

    {

    trace("stop");

    channel.stop();

    musicIsOn = false;

    } else 
        {

    playMusic();

    musicIsOn = true;

        }

} 

function playMusic():void
{

trace("play");

channel = music.play();
//play music through the SoundChannel

channel.addEventListener(Event.SOUND_COMPLETE, repeat);
//add an event listener that reacts when the sound being played is completed and repeats the sound
} 

function repeat(evt:Event):void 
    {

if(channel != null)

        {

channel.removeEventListener(Event.SOUND_COMPLETE, repeat);
//without removing the event listener the sound clip will play only twice
//I am not sure why but it feels like the event listener gets disposed of
//once used by default.

playMusic();

    }

        }

```
