?
Solved

as3 play function when sound stops

Posted on 2011-03-17
8
Medium Priority
?
469 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
Independent Software Vendors: 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 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