My problem is that I have an option page which contains the button to control the background music on and off. it works fine. But when I exit the option page then go back to option page again and play the music again, there will be two sound playing and if I stop it, only one sound will stop and the other one keeps on playing.
Here is my code please help me fix this thing thanks :) By the way Im using AS3.
to illustrate:

> ===================
> [   OPTION       ]        () is the on and off button of my music
> [ MUSIC: ()      ]       when I on the music it plays
> [                ]        then when I go to the main menu, the music plays well
> [ BACK           ]         no problem in this part 
> ===================
> 
> =====================
> [                  ]       in the main menu, music is playing well
> [  MAIN MENU       ]       if I go to the option page again
> [                  ]        ....................
> [ PLAY OPTION ETC  ]
> ===================
> 
> ===================
> [                ]     Here it goes, when I click the on and off button again to stop the music
> [  MUSIC: ()     ]     It didnt stop, instead it plays another music
> [                ]     So my problem is that. Please help me solve this
> [ BACK           ]
> ===================
> 





on_off_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound); // on_off_btn is the instance name

var fl_SC:SoundChannel;

//This variable keeps track of whether you want to play or stop the sound
var fl_ToPlay:Boolean = true; 

function fl_ClickToPlayStopSound(evt:MouseEvent):void
{   
    if(fl_ToPlay)
    {
        var s:Sound = new Sound(new URLRequest("harry.MP3"));
        fl_SC = s.play();
    }
    else
    {
        fl_SC.stop();
    }
    fl_ToPlay = !fl_ToPlay;
}

Because you're creating a new Sound whenever the user clicks play. Create the sound first, outside of the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.