How can you stop a sound from playing in as3?

33,220

Solution 1

You have to use SoundChannel class for this:

var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("myFavSong.mp3"));
myChannel = mySound.play();

// ...

myChannel.stop();

Reference: http://www.republicofcode.com/tutorials/flash/as3sound/ (Section 4 - Stopping a Sound)

Solution 2

//*This code for playing the sound after import to the library*
var mySound:Sound = new MenuMusic(); 
var myChannel:SoundChannel = new SoundChannel();
myChannel = mySound.play();

//*This code for the nextframe button*
btn_easy.addEventListener(MouseEvent.MOUSE_DOWN, btneasy);
function btneasy(event:MouseEvent):void
{
   gotoAndPlay(62);
   myChannel.stop();
}

Reminder :

You don't have to drag the sound file to the timeline . Just paste the code to the designated Action Script of the button.

var mySound:Sound = new MenuMusic();

'MenuMusic' can be changed what you want. Just Right click the sound file in the library then select Properties . Click ActionScript Tab and check 'Export for ActionScript' and you can now changed the name in 'Class' Input Box..

It works so fine to me :)

Hope it solved the problem ! :D

Share:
33,220
RockHopper
Author by

RockHopper

Updated on July 05, 2022

Comments

  • RockHopper
    RockHopper almost 2 years

    I have this code in the second frame to play a song, and it works fine. The class of the sound in my library being "MySound".

    var snd:MySound = new MySound
    snd.play();
    

    Now I need the song to stop playing from a latter frame.