Question

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;
}
Was it helpful?

Solution

Per local / network access, assure Flash Publish Settings "Local playback security" is set to "Access network only" instead of "Access local files only"

Also assure your path matches the environment of your hosted deployment. If you reference "music.mp3" as the URL, than "music.mp3" must reside at the same location as your published SWF.

Per preloading the mp3 instead of streaming, listen for Event.COMPLETE before calling play() of your sound:

var soundClip:Sound;

function init() {
    soundClip = new Sound();
    soundClip.load(new URLRequest("<path to sound file>"));
    soundClip.addEventListener(Event.COMPLETE, soundLoaded);
    soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);
}
init();

function soundLoaded(e:Event) {
    soundClip.play();
}

function soundLoading(e:ProgressEvent) {
    // preloader information goes here
}

If you do not need to stream the mp3, you could also just embed it in the library:

sound-clip

In which the sound could be played by:

var soundClip:Sound = new SoundClip(); 
soundClip.play();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top