Solved

as3 play function when sound stops

Posted on 2011-03-17
8
467 Views
Last Modified: 2012-05-11
I'm trying to get a function to work when audio stops.

I got this code from another posting. I put it in a flash file and it plays the audio but doesn't give me the trace when it's done. Can anybody help me??  IT seemed to work for the person who posted the question. Do you need to do something to the audio?

import flash.media.Sound;
import flash.net.URLRequest;
var soundReq:URLRequest = new URLRequest("test_05.mp3");
var sound:Sound = new Sound();
sound.load(soundReq);
 
sound.addEventListener(Event.COMPLETE, onComplete);
sound.addEventListener(Event.SOUND_COMPLETE, soundFinished);
 
function onComplete (event:Event):void {
      sound.play();
}
 
function soundFinished (event:Event):void {
      trace("my sound finished!");
}
0
Comment
Question by:designaire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 35158595
Can you attach your MP3 file?
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35158777
Never mind.
The my answer is:

You need to create a SoundChannel  to handle end your sound.

var channel:SoundChannel = new SoundChannel();
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundFinished);

Open in new window

0
 

Author Comment

by:designaire
ID: 35159083
This didn't seem to work either....I'm new at this...did I do something wrong. That's all I have in the flash file. Again it plays but doesn't show the trace when it's done.



import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.events.MouseEvent;

var channel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
var sound:Sound = new Sound();
sound.load(soundReq);
 
sound.addEventListener(Event.COMPLETE, onComplete);
sound.addEventListener(Event.SOUND_COMPLETE, soundFinished);
 
function onComplete (event:Event):void {
      channel = sound.play();
}
 
function soundFinished (event:Event):void {
      trace("my sound finished!");
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Accepted Solution

by:
dgofman earned 125 total points
ID: 35159107
You didn't apply my suggestion:

import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.events.MouseEvent;

var channel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
var sound:Sound = new Sound();
sound.load(soundReq);
 
sound.addEventListener(Event.COMPLETE, onComplete);

function onComplete (event:Event):void {
      var channel:SoundChannel = new SoundChannel();
      channel = sound.play();
      channel.addEventListener(Event.SOUND_COMPLETE, soundFinished);
}
 
function soundFinished (event:Event):void {
      event.target.removeEventListener(Event.SOUND_COMPLETE, soundFinished);
      trace("my sound finished!");
}
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35159126
Sorry you did but in wrong place :)

//var channel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
0
 

Author Comment

by:designaire
ID: 35159318
Thanks, it works but...

You added event.target.removeEventListener but it still worked with channel.addEventListener. Does the removeEventListener just cleaner?
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35159462
I just force to GC to remove this function. Not necessary becausewe created SoundChannel as local variable but is good practice.
0
 

Author Closing Comment

by:designaire
ID: 35159611
Works great, thanks!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to record live broadcast.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question