views:

127

answers:

2

I am writing a basic video player in Flash CS5 and Actionscript 3. For this basic player, I attach my NetStream to my NetConnection, then call the stream's .play() method to begin loading. Although I want the metadata and for the stream to begin buffering, I do not wish to start playing right away, so I immediately call the stream's .pause() method. Unfortunately, when I pause immediately, my stream's client's onMetaData event is not always called, so I don't necessarily get the total playtime of the loaded video.

As a workaround, I put the call to the "pause" method inside the onMetaData listener, but sometimes my video will have played a bit before receiving it's metadata, and will therefore continue to play until it does.

Is there a good way to stop my stream from playing, and still get my video metadata?

+1  A: 

You should call pause when the NetStatusEvent.STATUS event NetStream.Play.Start is fired.

Update:

For very short streams (e.g. buffer > duration) NetStream.Play.Start is likely to get fired just before the onMetaData callback.

Before pausing on NetStream.Play.Start, check if metaData has been provided, if not don't pause straight but await onMetaData to pause (just set a flag, e.g pauseOnMetaData = true).

Theo.T
The problem is, the NetStream.Play.Start event can fire prior to receiving the MetaData. I don't think this guarantees that I will have MetaData when I call pause, and pausing will prevent it from ever receiving.
Slobaum
eventhough you call pause at this point, the stream continues buffering and you will still receive the metadata. Both events should be handled separately.
Theo.T
Hhmm. I felt like when I was pausing at that point, I was never receiving metadata. Thanks, I'll give this a try.
Slobaum
A: 

Okay, here's a neat little way of thinking about this differently... Do not attach your video object to your stream object right away. Start your stream playing while showing a "please wait" visual WITHOUT your video object being displayed. In your onMetaData listener, see if you have stored a duration previously. If not, assume this is the first call to onMetaData, store the duration, pause playback, seek the stream to 0, THEN attach the video object.

The user will see a "please wait" for just a sec, then the video will appear, paused & ready to be played with it's duration times as expected. The user will be completely unaware that the stream played forward a bit while they were waiting.

Slobaum
if you do that, you also need to set the SoundTransform of the NetStream to 0 volume, or you may hear sound from the video while it is playing to get the metadata.
DanK
HA! Thanks @DanK, I'm working on a system without audio output (speakers or headphones), so that was definitely a nasty little oversight. ;)
Slobaum