鼠标停止播放音乐

时间:2011-05-10 13:02:43

标签: flash actionscript-3 events

我有一个附加了这个脚本的movieclip(在悬停时播放声音剪辑) - 问题是如果我将鼠标移开,我需要停止声音剪辑。现在它只是在它仍在播放时再次启动(鼠标悬停)==不好。

有没有人有解决方案?我试图制作MOUSE_OUT事件和.stop();,但它似乎不起作用。谢谢!

import flash.media.Sound;
import flash.media.SoundChannel;

//Declare a BeepSnd sound object that loads a library sound.
var BeepSnd:BeepSound = new BeepSound();
var soundControl:SoundChannel = new SoundChannel(); 

somebutton.addEventListener(MouseEvent.MOUSE_OVER,playNoises);
somebutton.addEventListener(MouseEvent.MOUSE_OUT,stopNoises);

function playNoises(event:Event){
    playSound(BeepSnd);
}

function playSound(soundObject:Object) {
    var channel:SoundChannel = soundObject.play();
}

function stopNoises(event:Event){
    stopSound(BeepSnd);
}

function stopSound(soundObject:Object) {
    var channel:SoundChannel = soundObject.stop();
}

我收到此错误:

TypeError: Error #1006: stop is not a function.
at radio_fla::MainTimeline/stopSound()
at radio_fla::MainTimeline/stopNoises()

3 个答案:

答案 0 :(得分:4)

您需要保留对播放SoundChannel时创建的Sound的引用。 Sound表示声音,而SoundChannel表示声音的播放,并且您要停止播放。

import flash.media.Sound;
import flash.media.SoundChannel;

//Declare a BeepSnd sound object that loads a library sound.
var BeepSnd:BeepSound = new BeepSound();
var soundControl:SoundChannel;

somebutton.addEventListener(MouseEvent.MOUSE_OVER,playNoises);
somebutton.addEventListener(MouseEvent.MOUSE_OUT,stopNoises);

function playNoises(event:Event){
    playSound(BeepSnd);
}

function playSound(soundObject:Object) {
    soundControl = soundObject.play();
}

function stopNoises(event:Event){
    stopSound();
}

function stopSound() {
    if (soundControl) {
        soundControl.stop();
        soundControl = null;
    }
}

答案 1 :(得分:1)

好的,问题是你实际上必须在通道对象上调用stop方法,而不是在声音对象上调用:channel.stop()。您也可以考虑使用ROLL_OVER/OUT代替MOUSE_OVER/OUT,但这当然与您的问题无关。

答案 2 :(得分:1)

尝试使用MouseEvent.ROLL_OVER和MouseEvent.ROLL_OUT代替MOUSE_OVER和MOUSE_OUT。