Jump to content
Search Community

How to know if a VideoLoader's video is ready

Ahrengot test
Moderator Tag

Recommended Posts

Hey!

 

I was wondering what the best way to know if my VideoLoader's video is ready for playback when autoPlay is set to false. Right now i'm listening to NetStatusEvent's from the VideoLoader's netstream like so:

 

var loader:VideoLoader = new VideoLoader(url, {autoPlay:false});
loader.load();

loader.netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetstatus);

function onNetstatus(e:NetStatusEvent):void 
{
if (e.info.code == "NetStream.Play.Start" || loader.bufferProgress == 1) 
{
	loader.netStream.removeEventListener(NetStatusEvent.NET_STATUS, onNetstatus);
	dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.READY));
}
}

note: VideoPlayerEvent is my own event class i'm using for various video players.

 

It's working as it should, but i'm wondering if i'm prone to any errors doing it this way or if any of your LoaderEvent's does the same thing.

 

Otherwise, would it be possible to have you add a READY event that get's dispatched whenever a video is ready for playback (much like YouTube does through it's API).

Link to comment
Share on other sites

When i say ready i mean that the video has buffered enough to start playback. Preferably buffered the amount described by the bufferTime property.

 

I tried the BUFFER_FULL event, but that doesn't seem to dispatch when autoPlay is set to false.

 

I created a little example to show what i mean.

 

(note i add a CLICK listener after onInit is fired, that makes the video play/pause whenever the stage is clicked, to show that BUFFER_FULL is only dispatched after play is called the first time)

package  
{
import com.greensock.events.LoaderEvent;
import com.greensock.loading.VideoLoader;

import flash.display.Sprite;
import flash.events.*;

public class Main extends Sprite
{
	private var _loader:VideoLoader;

	public function Main() 
	{
		if (stage) init();
		addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
	}

	private function init(e:Event = null):void 
	{
		removeEventListener(Event.ADDED_TO_STAGE, init);

		loadVideo();
	}

	private function loadVideo():void
	{
		_loader = new VideoLoader("assets/video/surfer.mp4", { container:this, autoPlay:false, onInit:onMetaData });
		_loader.addEventListener(VideoLoader.VIDEO_BUFFER_FULL, onEventRecieved);
		_loader.load();
	}

	private function onMetaData(e:Event):void
	{
		stage.addEventListener(MouseEvent.CLICK, onClickStage);
		buttonMode = true;
	}

	private function onClickStage(e:MouseEvent):void 
	{
		(_loader.videoPaused)? _loader.playVideo() : _loader.pauseVideo();
	}

	private function onEventRecieved(e:Event):void 
	{
		trace(this + " Video fully buffered!");
	}
}
}

 

Here's my example:

Link to comment
Share on other sites

Yes indeed - looks like Flash's NetStream object doesn't dispatch any indication of the buffer being full unless it is playing. Not to worry, though - I've worked around the issue in the latest version of VideoLoader (just posted a few minutes ago). Please download version 1.19 and let me know if that works well for you. Thanks for pointing this out.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...