Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Audio will not play with AS 3

Community Beginner ,
Oct 21, 2015 Oct 21, 2015

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

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 21, 2015 Oct 21, 2015

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.addEventListene

...
Translate
Community Expert ,
Oct 21, 2015 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2015 Oct 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2015 Oct 21, 2015

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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2015 Oct 21, 2015

Thanks so much for your help today with solving this problem. I appreciate you taking the time to help. I guess I'm pretty rusty in AS3. Your code has really helped me; when I published the file I got an Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.

Any clue why this would be happening?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2015 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2015 Oct 21, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2015 Oct 21, 2015

if you don't have those mp3's to load, then this line will trigger that error:

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


you need to replace that if you want to play library sounds.  first assign class names to each sound (eg, Audio_1, Audio_2 etc).

then use:


var C:Class=Class(getDefinitionByName("Audio_"+this.currentFrame);

mySound=new C();

myChannel=mySound.play()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2015 Oct 21, 2015

I replaced the old code with the code above, and it keeps repeating the same Audio_1 file with every click.

Thanks so much for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 22, 2015

you should have:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel;

var mySound:Sound=new Sound();

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 {

var C:Class=Class(getDefinitionByName("Audio_"+this.currentFrame);

mySound=new C();

myChannel=mySound.play()

prev_btn.visible=next_btn.visible=true;

    if (currentFrame == 1) {

        prev_btn.visible = false;

    } else  if (currentFrame == totalFrames) {   

        next_btn.visible = false;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2015 Oct 22, 2015

Awesome, yes, not sure what i had incorrect put into place, but its now working properly. Do I also need to add mySound.stop(); to stop the other sounds from playing at the same time once you've clicked on the next/prev buttons? They load when clicking on the buttons but the previous audio files continue. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 22, 2015

to stop the previous sound, use:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel;

var mySound:Sound=new Sound();

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 {

var C:Class=Class(getDefinitionByName("Audio_"+this.currentFrame);

mySound=new C();

myChannel.stop();

myChannel=mySound.play()

prev_btn.visible=next_btn.visible=true;

    if (currentFrame == 1) {

        prev_btn.visible = false;

    } else  if (currentFrame == totalFrames) {   

        next_btn.visible = false;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2015 Oct 22, 2015

I tried that earlier, and I thought it would have worked. I added it where you'd suggested and I get an error and it wont run the audio now.

"TypeError: Error #1009: Cannot access a property or method of a null object reference.

    at xxx_10202015_test_fla::MainTimeline/listenMe()[xxx_10202015_test_fla.MainTimeline::frame1:27]

    at xxx_10202015_test_fla::MainTimeline/frame1()[xxx_10202015_test_fla.MainTimeline::frame1:9]"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 22, 2015

use:

kglad wrote:

to stop the previous sound, use:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

var myChannel:SoundChannel;

var mySound:Sound=new Sound();

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 {

var C:Class=Class(getDefinitionByName("Audio_"+this.currentFrame);

mySound=new C();

if(myChannel){

myChannel.stop();

}

myChannel=mySound.play()

prev_btn.visible=next_btn.visible=true;

    if (currentFrame == 1) {

        prev_btn.visible = false;

    } else  if (currentFrame == totalFrames) {  

        next_btn.visible = false;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2015 Oct 22, 2015

Thank you!!!!!!!!!!!!!! You are amazing for helping me though this. It works and I will remember this for a very long time! You are a guru of AS3!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 22, 2015
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines