b-log – betriebsraum weblog

Software Development, Human-Computer Interaction, Projects…

Flash Video Tip 3: Stopping a FLV from downloading

March 3, 2007

When you set the contentPath property of the FLVPlayback component in Flash 8 it immediately starts (progressively) downloading the flv. Normally this is the desired behaviour but sometimes you don’t want the flv to take up bandwidth because there are currently other loading processes. Some time ago I read an article at Flashcomguru on how to generate a thumbnail preview of a video and stop the download process. While this should work if you manually create your NetConnection/NetStream object, the technique described there (trying to load a non-existent flv) doesn’t seem to work with the FLVPlayback component.

Here’s another approach (code is just on frame 1 for this demo, should be put into a class though):

Stage.scaleMode = "noScale";
 
import flash.display.*;
import mx.utils.Delegate;
 
var flvDummy:String = "dummy.flv";
var flvMovie:String = "trusted_computing.flv";
var snapBmd:BitmapData;
 
function onFlvReady(evObj:Object):Void {
	switch (fp.contentPath) {
		case flvMovie:
			createSnapshot();
			stopLoading();
		case flvDummy:
			//
			break;
	}
}
 
function onFlvProgress(evObj:Object):Void {
	//trace("flv loading: "+fp.contentPath+" "+evObj.bytesLoaded+" "+evObj.bytesTotal);
}
 
function createSnapshot():Void {
	snapBmd = new BitmapData(fp.width, fp.height, true, 0x00000000);
	snapBmd.draw(fp);
	snap_mc = this.createEmptyMovieClip("snap_mc", 1);
	snap_mc.attachBitmap(snapBmd, 0, "auto", true);
}
 
function stopLoading():Void {
	fp.contentPath = flvDummy;
	fp._visible = false;
	fp._x = -2000;
	fp._y = -2000;
}
 
function init():Void {
	fp.addEventListener("ready", Delegate.create(this, onFlvReady));
	fp.addEventListener("progress", Delegate.create(this, onFlvProgress));
	fp.volume = 0;
	fp.autoPlay = false;
	fp.contentPath = flvMovie;
}
 
init();

In the ready event (fired when the FLVPlayback component is ready to display the video) a snapshot is created by a BitmapData object and displayed on top of the video. The component is moved off screen so that it doesn’t consume too much processor power…

Then a dummy flv file is set as the contentPath which is just 280 bytes in size and almost immediately loaded – the dummy blocks further downloading of the main file. If you want to replay the video you need to start the main file, set the FLVPlayback visible, destroy the snapshot and prevent the ready event from creating a snapshot again.

Download the example files.

Filed under: Flex/AS3

3 Responses to “Flash Video Tip 3: Stopping a FLV from downloading”

  1. […] I was checking out fulasagoog today and found a posting from Betriebsraum weblog about stopping an FLV from downloading. This not only saves bandwidth costs, but also saves reduces processing power and memory usage. The basic concept is to create a snapshot of the video using the bitmapdata object, overlay this snapshot over the video, hide the video and replace the FLV player’s source with a tiny FLV. and creating a snapshot of video to put on top of the video. Utterly brilliant and probalby something that we should be using for Vongo. […]

  2. Mike says:

    This is EXACTLY what I’ve been looking for but when I download the example files and run the swf it just stays paused. How can make this play upon the push of a button e.g. youtube.

Add a comment

Follow comments to this post by subscribing to the comment feed.