在鼠标上停止播放音乐
我有一个将此脚本附加到它的动画片段(在悬停时播放声音片段) - 问题是,如果我将鼠标移出,我需要停止声音片段。现在它只是重新开始,而它仍然在播放(鼠标悬停)==不好。在鼠标上停止播放音乐
有没有人有解决方案?我试图做一个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()
在玩Sound
时,您需要保留对SoundChannel
的引用。 A 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;
}
}
工程就像一个魅力! – janhartmann 2011-05-10 13:43:21
好了,问题是,你确实有调用通道对象的停止方法,而不是声音对象:channel.stop()
。你也可以考虑使用ROLL_OVER/OUT
而不是MOUSE_OVER/OUT
,但这当然与你的问题无关。
尝试使用MouseEvent.ROLL_OVER和MouseEvent.ROLL_OUT而不是MOUSE_OVER和MOUSE_OUT。
我做了这也是,谢谢。 – janhartmann 2011-05-10 13:43:32
你能发布整个代码吗?你在哪里停止声音? – 2011-05-10 13:13:40
这是整个代码 - 没有我阻止它的地方。我需要加入这个。 :-) – janhartmann 2011-05-10 13:14:11
你说你尝试添加一个'MOUSE_OUT'事件和一个'.stop();',如果你发布该代码,也许有人可以告诉你为什么它不起作用 – 2011-05-10 13:16:42