Solved

as3 play function when sound stops

Posted on 2011-03-17
8
463 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Action Script commands 3 217
Adobe Flash Player not supported on my Tablet 7 730
blocked plugin flash 5 117
Adobe indesign suite cs5.5   How to enable "indesign" capability? 2 199
First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

786 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