How can you stop a sound from playing in as3?

烈酒焚心 提交于 2019-12-10 19:11:38

问题


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.


回答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)




回答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



来源:https://stackoverflow.com/questions/7421939/how-can-you-stop-a-sound-from-playing-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!