Playing flv movies with AS3
Posted on: August 5th, 2009
This is a little example of how to play .FLV files in Flash.You can use pre-made components (AS3 FLVPlayback ) to do so, but sometimes, for whatever reason, you want to do it yourself.
Here’s the outcome… click on the movie to start and pause it
Let’s get startet…
First of all you need a .flv file. You can use www.keepvid.com to download files from youtube.
This is our main class, the “entering point” of our program:
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var p:Sprite = new Player();
p.playFlv("travis_sing.flv");
addChild(p);
}
}
}</pre>
<p style="padding-top:1.4em;">Next we have to create the Player.as class:</p>
<pre class="brush: as3;">package{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.utils.Timer;
import flash.display.Shape;
public class Player extends Sprite{
private var _video:Video;
private var _stream:NetStream;
private var _duration:uint;
private var _timer:Timer;
private var _pbFrame:Shape;
private var _pbDuration:Shape;
private var _pbLoaded:Shape;
public function Player(){
_pbFrame = new Shape();
_pbFrame.graphics.beginFill(0x000000, 1);
_pbFrame.graphics.drawRect(0, 300, 400, 7); //(x, y, width, height)
_pbFrame.graphics.endFill();
addChild(_pbFrame);
_pbLoaded = new Shape();
_pbLoaded.graphics.beginFill(0xAAAAAA, 1);
_pbLoaded.graphics.drawRect(0, 301, 1, 5); //(x, y, width, height)
_pbLoaded.graphics.endFill();
addChild(_pbLoaded);
_pbDuration = new Shape();
_pbDuration.graphics.beginFill(0xFFFFFF, 1);
_pbDuration.graphics.drawRect(0, 301, 1, 5); //(x, y, width, height)
_pbDuration.graphics.endFill();
addChild(_pbDuration);
_duration = 0;
_timer = new Timer(1000);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
public function playFlv(flvUrl){
_video = new Video(400,300);
var connection:NetConnection = new NetConnection();
connection.connect(null);
_stream = new NetStream(connection);
_stream.play(flvUrl);
var Client:Object = new Object();
Client.onMetaData = onMetaData;
_stream.client = Client;
_video.attachNetStream(_stream);
addEventListener(MouseEvent.CLICK, onVideoClick);
_stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
addChild(_video);
}
private function onVideoClick(e:Event){
_stream.togglePause();
}
private function onMetaData(data:Object){
_duration = data.duration;
_stream.pause();
}
private function onNetStatus(e:NetStatusEvent){
//trace("state:"+ e);
//_video.width = _video.videoWidth;
//_video.height = _video.videoHeight;
}
private function onTimer(t:TimerEvent){
if( _duration > 0 && _stream.time > 0 ){
_pbDuration.width = (_video.width/_duration)*_stream.time;
_pbLoaded.width = (_video.width/_stream.bytesTotal)*_stream.bytesLoaded;
}
}
private function asyncErrorHandler(e:AsyncErrorEvent):void {
trace("asyncErrorHandler:"+ e);
}
}
}
That’s it!
Posted in Flash | Trackback Url








No Responses to “Playing flv movies with AS3”
Trackbacks/Pingbacks
Leave a reply