Solved

as3 play function when sound stops

Posted on 2011-03-17
8
466 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
Industry Leaders: 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

734 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