I used the following script (AS3) I found online to play the music in my flash. I don't know actionscript.
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
function onComplete(evt:Event):void {
//Play loaded sound
sndChannel = soundClip.play();
isPlaying = true;
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}
As you can see in the script above, music_play
and music_stop
are instance names for the play and stop buttons. The .mp3 file, when loaded, is supposed to play the music, but the music plays only when I view it from Flash. But when I view it locally or online (http://ulfhc.com.au/), the music doesn't play. I'm sure its not because of the location of .swf or .mp3 files.
Would you please help me with this?
Thank you very much.
Edit:
So the new code would be this?
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
var soundClip:Sound;
function init() {
soundClip = new Sound();
soundClip.load(new URLRequest("music.mp3"));
soundClip.addEventListener(Event.COMPLETE, soundLoaded);
soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
}
init();
function onComplete(evt:Event):void {
sndChannel = soundClip.play();
isPlaying = true;
}
function soundLoaded(e:Event) {
soundClip.play();
}
function soundLoading(e:ProgressEvent) {
// preloader information goes here
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}