How do I play/stop an embedded music file in Flash/AS3 via a button?
This is what I have:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
var myMusic:Sound = new ThemeSong();
var channel:SoundChannel = myMusic.play();
startButton.addEventListener(MouseEvent.CLICK, onStartClick);
stopButton.addEventListener(MouseEvent.CLICK, onStopClick);
function onStartClick(event:MouseEvent):void{
myMusic.play();
}
function onStopClick(event:MouseEvent):void{
myMusic.stop();
}
ThemeSong is the name of the class I assigned for the music file (import -> libraries -> properties -> export for AS -> class name). startButton and stopButton are the identifiers for the buttons.
If I run this I'll get an error saying:
"Scene 1, Layer 'actions', Frame 1, Line 69 1061: Call to a possibly undefined method stop through a reference with static type flash.media:Sound."
Thanks.
You're close Flash is wonky sometimes:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
var myMusic:Sound = new ThemeSong();
var channel:SoundChannel = myMusic.play();
startButton.addEventListener(MouseEvent.CLICK, onStartClick);
stopButton.addEventListener(MouseEvent.CLICK, onStopClick);
function onStartClick(event:MouseEvent):void{
channel = myMusic.play();
}
function onStopClick(event:MouseEvent):void{
channel.stop();
}