topical media & game development
actionscript-video-10-thumb-FLVBitmapThumb.ax
actionscript-video-10-thumb-FLVBitmapThumb.ax
[swf]
flex
package
{
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.NetStatusEvent;
import flash.geom.Matrix;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class @ax-actionscript-video-10-thumb-FLVBitmapThumb
{
public static var SCALE:Number = 0.3; // for a thumbnail a quarter the size of the video
private var nc:NetConnection;
private var ns:NetStream;
private var target_mc:MovieClip;
private var thumb_mc:MovieClip;
private var bitmap:Bitmap;
private var bitmapData:BitmapData;
private var wasThumbTaken:Boolean;
public function @ax-actionscript-video-10-thumb-FLVBitmapThumb (target:MovieClip)
{
target_mc = target;
wasThumbTaken = false;
// Create and open a new net connection
nc = new NetConnection();
nc.connect(null);
// Create a new stream object using the net connection
ns = new NetStream(nc);
ns.client = this;
// Attach the stream object to the actionscript-controlled
// video component you've placed on the stage with an
// instance name of myVid
target_mc.myVid.attachNetStream(ns);
// Begin playing the stream
ns.play("../assets/video/flash/sl_lg.flv");
// wait 1 second, then initiate creation of thumb bitmap holder
var delayTimer:Timer = new Timer(1000, 1);
delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, makeThumbBitmap);
delayTimer.start();
// Callback handler invoked every time a status change or error is
// posted for the NetStream object
ns.addEventListener(NetStatusEvent.NET_STATUS, handleOnStatus);
}
public function onMetaData (info:Object):void
{
trace("stream metadata: ");
for (var prop:String in info) {
trace("\t" + prop + ": " + info[prop]);
}
}
private function makeThumbBitmap(evt:TimerEvent):void {
// Set the width and height of the thumbnail
var bmwidth:int = Math.round(target_mc.myVid.width*SCALE);
var bmheight:int = Math.round(target_mc.myVid.height*SCALE);
// Create a new BitmapData object and fill with grey for now
bitmapData = new BitmapData(bmwidth, bmheight, false, 0x00CCCCCC);
bitmap = new Bitmap(bitmapData);
// Create a new movieclip to hold our bitmap, attach the bitmap
thumb_mc = new MovieClip();
thumb_mc.addEventListener (MouseEvent.CLICK, chooseVideo);
thumb_mc.addChild(bitmap);
// Add the thumbnail to the target
target_mc.addChild(thumb_mc);
// Specify its coordinates relative to the video object, then make it clickable
thumb_mc.x = target_mc.myVid.x + target_mc.myVid.width + 10;
thumb_mc.y = target_mc.myVid.y;
thumb_mc.buttonMode = true;
thumb_mc.useHandCursor = true;
}
public function chooseVideo(event:MouseEvent):void {
// Start the video if it is paused
ns.togglePause();
}
public function handleOnStatus(event:NetStatusEvent):void
{
trace("stream status: ");
// Cycle through the properties to check that the buffer is full
// then take a bitmap snapshot, scale it using a matrix.scale
// method, then pause the video and change the global variable
// thumbTaken to true
var info:Object = event.info;
for (var prop:String in info) {
trace("\t" + prop + ": " + info[prop]);
if(info.code == "NetStream.Buffer.Full") {
if(!wasThumbTaken) {
var scalematrix:Matrix = new Matrix();
scalematrix.scale(SCALE,SCALE);
bitmapData.draw(target_mc.myVid, scalematrix);
ns.pause();
wasThumbTaken = true;
}
}
}
}
public function destroy():void {
// Always good practice to have a destroy method to have
// an easy way to dispose of bitmaps when you're done with them
bitmapData.dispose();
thumb_mc.removeEventListener (MouseEvent.CLICK, chooseVideo);
target_mc.removeChild(thumb_mc);
}
}
}
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.