如何在 Action Script 3 中停止播放歌曲

How to stop the song from playing in Action Script 3

我试图在用户点击按钮时停止播放歌曲,这是我的代码:

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);
}

当我单击 animation_play 对象时,它工作得非常好,它通过播放声音并从指定的帧开始动画来实现应有的效果。然而,如果我点击 animation_stop 对象,我会得到一个错误

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

有人知道如何解决这个问题吗?

要停止声音,您必须使用这样的 SoundChannel 对象,例如:

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

function stopSound(event:Event): void  
{
    cmyChannel.stop();
}

希望能帮到你。

您需要将 mySound.play() 设置为 cmyChannel 对象。然后在 cmyChannel 上调用 stop。这是代码:

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);
}