PDA

View Full Version : Add event listener to playback


gani
12-02-2008, 02:02 PM
I have the following actionscript which plays an mp3.


var s:Sound = new Sound();
var sc:SoundChannel;

s.load("my_song.mp3");
sc = s.play();

import flash.external.ExternalInterface;
ExternalInterface.call("alert", sc.position);

I want the alert call to be activated every 100ms during the playback. I have no idea how to do this. I need to add some sort of eventlistener to the playback so that it triggers the alert every 100ms... Any ideas?

gnomeontherun
12-02-2008, 02:56 PM
If you just want to have a function repeat, then do something like this:

updateMarker = setInterval(functionname,100);

http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary646.html

The only trick then would be to make sure that when the audio stops to clear the interval so it stops running repeatedly.

clearInterval(updateMarker );

http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary141.html

I'll post more later when I have Flash files around with examples of listeners.

gani
12-02-2008, 03:07 PM
That wouldn't work.... if anything happens to the playback ... then the alerts will continue coming. Or if the playback is delayed a bit ...the alerts will come out of sync. :(

gnomeontherun
12-02-2008, 03:24 PM
Ok, I didn't know exactly what the function was meant to do, I assumed it was something like adjusting the marker position or something like that.

Anyways have you looked at the audio tutorials at www.gotoandlearn.com? I suggest those first, because they can better explain with visuals!

gani
12-02-2008, 03:26 PM
ok...will get back to you ..let me go and have a look see....

gani
12-02-2008, 03:56 PM
strange ...it seems no one has ever done this before ...i thought it would be a common thing :(

gnomeontherun
12-02-2008, 04:28 PM
I haven't done it before, and I was having trouble finding good documentation on AS2 scripts. Here is something though.

http://www.adobe.com/devnet/actionscript/articles/cue_points_audio_02.html

http://flash-reference.icod.de/Sound.html

gani
12-02-2008, 05:37 PM
ok, half of that was impossible for my pea brain to understand... the other half did not have what i was looking for. :o

I guess the only solution, which i was trying to avoid is to do a setInterval which will run every 50ms to check the playback position. If the playback position is within certain limits then it will alert the message.

I wish i had found a more intelligent work around though :o

gnomeontherun
12-02-2008, 10:15 PM
The first article is a bit complex I understand, but it covers making a class which is then used to provide information about the audio file. Are you planning on streaming this audio? I would think that if you buffer the item it would be fine in most situations, but what are you sending to the alert?

Also there is a .onSoundComplete event which allows you to clear the interval when this occurs, so the alert stops.

gani
12-03-2008, 10:05 AM
the alert will actually be a gotoandStop function which will show a particular frame depending on the position of the sound being played. I know this can be done in AS3... but this is an AS2 solution.

gnomeontherun
12-03-2008, 12:24 PM
You could use cue points in the audio file (which is the first that I liked to, which is likely the one that caused you some headaches), and here is a great guide to audio (without cues) that I just came across. I just can't find any good resources either for cues besides that one article.


http://www.kevinminke.com/?p=3

gani
12-04-2008, 09:39 AM
There is no tutorial on cuepoints for actionscript 3 mp3 playback :(.. i have searched for days.

oh well.... thanks anyway Jeremy

gani
12-07-2008, 03:59 PM
talk about ignorance...thanks jeremy...it seems your sent me what i was looking for in the first place :thumbsup: