Skip to main content
dmpearson
Participating Frequently
October 21, 2015
Answered

Audio will not play with AS 3

  • October 21, 2015
  • 1 reply
  • 1681 views

Hi,  I'm using Adobe Flash CC 2015 and I have an issue that is driving me mad. I've used the old code from a working file (which apparently works in the container) but when I run it on my computer, it won't run properly when published. It plays the file, but the audio keeps looping over itself. It does not call for looping anywhere in the file properties. Here is my code:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel = new SoundChannel();

this.addEventListener(Event.ENTER_FRAME, listenMe);

next_btn.addEventListener(MouseEvent.CLICK, clickNext);

prev_btn.addEventListener(MouseEvent.CLICK, clickPrev);

function clickNext(e:MouseEvent):void {

    myChannel.stop();

    gotoAndStop(currentFrame+1);

}

function clickPrev(e:MouseEvent):void {

    myChannel.stop();

    gotoAndStop(currentFrame-1);

}

function listenMe(e:Event) {

    if (currentFrame == 1) {

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

mySound.load(new URLRequest("audio_1.mp3"));

myChannel = mySound.play();

        prev_btn.visible = false;

        stop();

    } else {

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

mySound.load(new URLRequest("audio_2.mp3"));

myChannel = mySound.play();

        prev_btn.visible = true;

    }

    if (currentFrame == totalFrames) {

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

mySound.load(new URLRequest("audio_3.mp3"));

myChannel = mySound.play();       

        next_btn.visible = false;

    } else {

        next_btn.visible = true;

    }

}

I know there is some repetitive code, but its in the original file that I'm updating and it apparently works, but not on my computer. I'm using a MAC Book Pro but running Windows 10 on it. Not sure if that is causing the problem or not. but other files seem to be running just fine.

Any help would be appreciated as I haven't used Flash in over a year.

Dee

This topic has been closed for replies.
Correct answer kglad

i'm assuming there's something visual on each timeline frame that makes it important for your project to change frames (otherwise, there's superfluous code).  use:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel;

var mySound:Sound=new Sound();

////////////// this.addEventListener(Event.ENTER_FRAME, listenMe);  comment-out this line

listenMe();

prev_btn.visible=false;

next_btn.addEventListener(MouseEvent.CLICK, clickNext);

prev_btn.addEventListener(MouseEvent.CLICK, clickPrev);

function clickNext(e:MouseEvent):void {

nextFrame();

listenMe();

}

function clickPrev(e:MouseEvent):void {

prevFrame();

listenMe();

}

function listenMe():void {

mySound.load(new URLRequest('audio_'+this.currentFrame+'.mp3'));

myChannel=mySound.play();  // i don't see where you use myChannel, but if you need it, this is where it should be assigned

prev_btn.visible=next_btn.visible=true;

    if (currentFrame == 1) {

        prev_btn.visible = false;

    } else  if (currentFrame == totalFrames) {    

        next_btn.visible = false;

    }

}

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

1 reply

kglad
Community Expert
Community Expert
October 21, 2015

you're calling listenMe repeatedly!  what do you expect to happen other than a excruciating audio mess?

in addition, myChannel is local to listenMe so you can't use it to stop your sound in clickNext or clickPrev.

remove that enterframe loop, call listenMe when you want to play a sound and retest.

dmpearson
dmpearsonAuthor
Participating Frequently
October 21, 2015

Thanks! Its been a while since I've used Flash... This is what i have now:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel = new SoundChannel();

this.addEventListener(Event.ENTER_FRAME, listenMe);

next_btn.addEventListener(MouseEvent.CLICK, clickNext);

prev_btn.addEventListener(MouseEvent.CLICK, clickPrev);

function clickNext(e:MouseEvent):void {

    gotoAndStop(currentFrame+1);

}

function clickPrev(e:MouseEvent):void {

    gotoAndStop(currentFrame-1);

}

function listenMe(e:Event):void {

var mySound:Sound = new Sound();

var myChannel:SoundChannel = new SoundChannel();

    if (currentFrame == 1) {

mySound.load(new URLRequest("audio_1.mp3"));

mySound.play();

        prev_btn.visible = false;

        stop();

    } else {

mySound.load(new URLRequest("audio_2.mp3"));

mySound.play();

        prev_btn.visible = true;       

    }

    if (currentFrame == totalFrames) {    

mySound.load(new URLRequest("audio_3.mp3"));

mySound.play();       

        next_btn.visible = false;

    } else {

        next_btn.visible = true;

        this.removeEventListener(Event.ENTER_FRAME, listenMe);

    }

}

It isn't looping anymore, I appreciate your help. I now have an issue where only the first audio will play. Have I missed something you mentioned?

dmpearson
dmpearsonAuthor
Participating Frequently
October 21, 2015

if your app starts on frame 1, there's no audio_1.mp3 in the path of the published files.  or you're publishing for html5 which doesn't seem likely.


It starts on frame 1,yes, and no HTML5. I should have mentioned that I have added the audio files in the .fla's library instead of an external location.