Stop music on mouse out
I have a movieclip which has this script attached to it (plays a soundclip on hover) - problem is that if I move the mouse out I need to stop the soundc开发者_JAVA技巧lip. Right now it just starts again while its still playing (on mouse over) == not good.
Does anyone have a solution? I tried to make a MOUSE_OUT
event and a .stop();
but it does not seem to work. Thank you!
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();
}
I get this error:
TypeError: Error #1006: stop is not a function.
at radio_fla::MainTimeline/stopSound()
at radio_fla::MainTimeline/stopNoises()
You need to keep a reference to the SoundChannel
created when playing a Sound
. A Sound
represents a sound, while a SoundChannel
represents the playback of a sound, and it is the playback, that you want to 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;
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;
}
}
Ok, the problem is that you actually have to call the stop method on the channel object, not on the sound object: channel.stop()
. Also you may consider using ROLL_OVER/OUT
instead of MOUSE_OVER/OUT
, but this has nothing to do with your problem of course.
Try using MouseEvent.ROLL_OVER AND MouseEvent.ROLL_OUT instead of MOUSE_OVER and MOUSE_OUT.
精彩评论