0

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;
}
DriftingSteps
  • 526
  • 1
  • 7
  • 23
  • 1
    Check your Flash Pro Publish Settings under "Local playback security" whether it's "Access local files only" or "Access network only". – Jason Sturges Jul 20 '12 at 17:19
  • It's in 'Access local files only'. Should I change it to 'Access network only'? – DriftingSteps Jul 20 '12 at 17:28
  • 1
    Yes, so that it may access network resources when deployed to your website. – Jason Sturges Jul 20 '12 at 17:29
  • Thanks! Changing the playback security made the music play locally. But it doesn't seem to play online... maybe my internet too slow right now. I had another question too, if you don't mind. Is it possible to load the .mp3 first (with a preloader) and start the flash once the music is loaded? – DriftingSteps Jul 20 '12 at 19:10

1 Answers1

0

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();
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • Strurges, I've added what you wrote above, is that the right way to do it? And I also embedded the mp3 in the library but I couldn't get it wroking :( – DriftingSteps Jul 20 '12 at 21:39
  • If you embed it in the library, just instantiate it from the AS Linkage you specified. Like in my screen capture above would be: `var soundClip:Sound = new SoundClip(); soundClip.play();` – Jason Sturges Jul 20 '12 at 21:42
  • Then I won't need to add this `soundClip.load(new URLRequest(""));`, I believe? – DriftingSteps Jul 20 '12 at 22:02
  • No. If you embed the mp3 in your FLA library, there's no need to load it. Since you want to wait for the mp3 to be loaded before showing Flash content anyways, this would probably be easiest. – Jason Sturges Jul 20 '12 at 22:03
  • Okay, thank you very much! I'll see if I can get this working. – DriftingSteps Jul 20 '12 at 22:09
  • I couldn't get it working. I imported the sound clip to the library, gave it a Linkage class, applied it to the script, and it still doesn't play. Additionally, I had to add `var sndChannel:SoundChannel = new SoundChannel();` and comment out `soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);` since `soundLoading` didn't exist. The script just doesn't load the mp3. And I also get an error - `ArgumentError: Error #2068: Invalid sound. at flash.media::Sound/play(), at ulfhc_v_fla::music_controller_60/btnPressController()` – DriftingSteps Jul 20 '12 at 22:45