Solved

as3 play function when sound stops

Posted on 2011-03-17
8
459 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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. …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
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.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now