Progressive stream – onComplete

yleistäThis post is kind of related to the previous one. I think it was a bit misleading. I stated there that it is possible to detect NetStreams “onComplete” with onPlayStatus handler. And that is partially true. But not in case of using progressive stream (progressive download). I should have mentioned it but I didn’t know about that until today :).

It seems that there is no efficient way detect whether the playback has completed or not. At least there is no built-in logic for this in Flash (concerns only progressive stream). There is, however, at least one way to accomplish this task. And I’m pretty sure you’ve already guessed it. Comparing playback time with the total length of the file. Needles to say that there is nothing new about this but I wanted to create an easy way implement this feature in the code.

So I created new class called “MediaStream” (extends NetStream) and custom “StreamEvent” for you to use with it. This way you don’t have to play around with that nasty NetStream.client crap anymore.

OK… Enough with all this talking. Download the source from the link below (and move the class files to your Flash class path).

Let’s take a closer look to how it all works. (I am assuming you place the code in the first frame on Flash timeline)

var movieToPlay:String = "myMovie.flv";
var nc:NetConnection = new NetConnection();
var ns:MediaStream = new MediaStream(nc);

This is the basic stuff. Create a NetConnection object and prepare it for progressive stream. After that create a MediaStream object and pass the NetConnection object to it. Then it is time to set up the listeners and handlers for detecting metadata and onComplete. The metaData is available through the event objects data property.

ns.addEventListener(StreamEvent.COMPLETE, onComplete);
ns.addEventListener(StreamEvent.META_DATA, onMetaData);
function onMetaData(e:StreamEvent):void
	var prop:String;
	for(prop in
		trace(prop + " : " +[prop])
function onComplete(e:StreamEvent):void

And finally attach the stream to video instance and start the playback.


That should do it. I still want to emphasize that there is actually nothing new in this approach. But if you find it usefull you’re free use it. At least I’m going to ;).

4 thoughts on “Progressive stream – onComplete

  1. zackNo Gravatar

    nice, just what i was looking for. but it doesnt work for me 😉 . I tried with your example and changed the filename only. I got this as output:
    “videocodecid : -1
    canSeekToEnd : true
    duration : 0
    audiocodecid : -1
    Stream Complete!
    videocodecid : -1
    canSeekToEnd : true
    duration : 0
    audiocodecid : -1
    Stream Complete!
    videocodecid : -1″

    Repeating endlessly. It sounds quite horrible in the speakers. Flash has no problem reading the file if I use normal code to do this (but then I cant figure out when it stops). Any ideas?

    best regards,

  2. zackNo Gravatar

    *cant figure out when the playback is finished i mean. I.e, when the onPlayStatus stop event should have been fired 😉

  3. Timo VirtanenNo Gravatar Post author

    I assume that you are looping the flv file?

    How long the flv file is supposed to be. Now the duration property seems to be 0 so the the “onComplete” handler will trigger continuosly.

  4. Thomas AndersenNo Gravatar

    The script works for me if the videofile doesn’t have a soundtrack!
    I am using the exact same mp4 (H.264). But it only works when the sound is omitted from the videofile.
    Very strange!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>