designaire
asked on
as3 play function when sound stops
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(Eve nt.COMPLET E, onComplete);
sound.addEventListener(Eve nt.SOUND_C OMPLETE, soundFinished);
function onComplete (event:Event):void {
sound.play();
}
function soundFinished (event:Event):void {
trace("my sound finished!");
}
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(Eve
sound.addEventListener(Eve
function onComplete (event:Event):void {
sound.play();
}
function soundFinished (event:Event):void {
trace("my sound finished!");
}
Can you attach your MP3 file?
Never mind.
The my answer is:
You need to create a SoundChannel to handle end your sound.
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);
ASKER
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(Eve nt.COMPLET E, onComplete);
sound.addEventListener(Eve nt.SOUND_C OMPLETE, soundFinished);
function onComplete (event:Event):void {
channel = sound.play();
}
function soundFinished (event:Event):void {
trace("my sound finished!");
}
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(Eve
sound.addEventListener(Eve
function onComplete (event:Event):void {
channel = sound.play();
}
function soundFinished (event:Event):void {
trace("my sound finished!");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry you did but in wrong place :)
//var channel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
//var channel:SoundChannel = new SoundChannel();
var soundReq:URLRequest = new URLRequest("test_05.mp3");
ASKER
Thanks, it works but...
You added event.target.removeEventLi stener but it still worked with channel.addEventListener. Does the removeEventListener just cleaner?
You added event.target.removeEventLi
I just force to GC to remove this function. Not necessary becausewe created SoundChannel as local variable but is good practice.
ASKER
Works great, thanks!