开发者

How can you stop a sound from playing in as3?

开发者 https://www.devze.com 2023-04-05 10:21 出处:网络
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\".

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开发者_JS百科 snd:MySound = new MySound
snd.play();

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


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)


//*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

0

精彩评论

暂无评论...
验证码 换一张
取 消