Search code examples
actionscript-3adobeflash-cs6

How to stop the song from playing in Action Script 3


I am trying to stop the song from playing when user clicks on a button, this is my code :

var mySound:MainSound = new MainSound();
var cmyChannel :SoundChannel;

animation_play.addEventListener(MouseEvent.CLICK, playSound);

function playSound(event:Event) {
mySound.play();
}

animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:Event) {
mySound.stop();
}


animation_play.addEventListener(MouseEvent.CLICK,    fl_ClickToGoToAndPlayFromFrame_3);

function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
{
   gotoAndPlay(2);
}

when i click on the animation_play object it works perfectly fine, it does as how it should be by playing the sound and starting the animation from the specified frame. however if i click on animation_stop object i get an error

TypeError: Error #1006: stop is not a function.

Anyone know how to go about fixing this ?


Solution

  • You need to set the mySound.play() to the cmyChannel object. Then call stop on cmyChannel. Here is the code:

    var mySound:MainSound = new MainSound();
    var cmyChannel :SoundChannel;
    
    animation_play.addEventListener(MouseEvent.CLICK, playSound);
    
    function playSound(event:Event) {
    cmyChannel  = mySound.play();
    }
    
    animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
    function stopSound(event:Event) {
    cmyChannel.stop();
    }
    
    
    animation_play.addEventListener(MouseEvent.CLICK,    fl_ClickToGoToAndPlayFromFrame_3);
    
    function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
    {
       gotoAndPlay(2);
    }