Barefoot Development

Concerning Media Components and Cue Points

On a recent Flash project (version 7) with talking characters, it became necessary to start and stop animations at specific points during the playback of a sound file. We decided that the most efficient way to manage the calls was via CuePoints which are available in MX 2004 Pro through the Media components.

I placed an instance of the MediaDisplay component on the stage and initialized it thusly:


var myMedia = _root.my_media_component;
myMedia.autoPlay = false;

// Load some sound into the component
// which has been preloaded
myMedia.setMedia("some.mp3", MP3);

// Add CuePoints to the component
myMedia.addCuePoint("start", 7);
myMedia.addCuePoint("stop", 19);

// Create a Cue Point Listener object
var cuePointListener:Object = new Object();

// Call this method when a CuePoint is "heard"
cuePointListener.cuePoint = function(eventObject):Void {
// call the function with the CuePoint's name
handleCuePoint(eventObject.target.mostRecentCuePointName);
}

// Call this method when the sound is complete
cuePointListener.complete = function(eventObject):Void {
trace("I'm all done.");
// do some stuff
}

// Associate/attach the listers to the component
myMedia.addEventListener("cuePoint",cuePointListener);
myMedia.addEventListener("complete", cuePointListener);

// Method to handle each CuePoint
// @param myCue: The name of the "heard" CuePoint
function handleCuePoint(myCue:String):Void {
switch (myCue) {
case "start" :
trace("Start me up.");
// start some stuff
break;
case "stop" :
trace("Stop it, now.");
// stop some stuff
break;
}
}

// Now that everything is ready to go, let's play!
myMedia.play();
trace("The beginning.");


Okay, that was pretty simple -- the sound is playing great and the CuePoints are called in time. But wait, what was that? The sound finished and my CuePoints just fired again. Let's take a look at the output:


The beginning
Start me up.
Stop it, now.
Start me up.
Stop it, now.
I'm all done.


So, something is amiss between the completion of the sound and our listener "hearing" the complete. If we trace myMedia.playheadTime in the complete function, it returns 0. The Media component is automatically rewinding the sound when it finishes but before triggering the complete listener. Blockbuster would be proud. This wouldn't be so bad, but the act of rewinding fires the CuePoints as the sound scrubs back to 0. Luckily, the solution is simple. After a CuePoint is heard, just remove it:


// Call this method when a CuePoint is "heard"
cuePointListener.cuePoint = function(eventObject):Void {
// call the function with the CuePoint's name
handleCuePoint(eventObject.target.mostRecentCuePointName);
// now that the function above has been called, remove the CuePoint
myMedia.removeCuePoint(mediaStreamer.getCuePoint(eventObject.target.mostRecentCuePointName));
}


Not much to it, but the behavior of the MediaDisplay component is so odd that I just had to share.

Bobby Uhlenbrock, Application Developer, Barefoot

Labels: ,

1 comments

  1. Blogger Unknown said:  

    Wow, that is very useful, and I managed to come across it JUST as I needed. Although, I noticed another quirk in the Media Controller.
    If you pause in the middle of a song and then start a NEW song. It play the song but the bar will show paused. So when you try to pause the newly playing song, it restarts it instead. I cant think of a work around for that.

Post a Comment

« Home