Demo Showing How to Play a FLV File
This is a demo showing how to implement a video player in AS3. Click on the buttons below to play, pause or stop the video. Code is presented below.
Source Code
import fl.controls.Button;
import fl.controls.Slider;
import fl.events.ComponentEvent;
import fl.events.SliderEvent;
import flash.ui.ContextMenu;
var firstRun = true;
// Step 1: Create new connection
var connection : NetConnection = new NetConnection();
connection.connect(null);
// Step 2: Create a stream
var stream : NetStream = new NetStream(connection);
// Step 3: Create a video
var vid : Video = new Video();
addChild(vid);
vid.attachNetStream(stream);
stream.receiveVideo(true);
stream.receiveAudio(false);
// Step 4: Add video event handlers
stream.addEventListener(NetStatusEvent.NET_STATUS, netstat);
var netClient : Object = new Object();
netClient.onMetaData = netmeta;
stream.client = netClient;
// Play button
var bPlay : Button = new Button;
bPlay.label = "Play";
bPlay.addEventListener(ComponentEvent.BUTTON_DOWN, playButton);
bPlay.x = 10; bPlay.y = 265; bPlay.width = 45;
addChild(bPlay);
// Pause button
var bPause : Button = new Button;
bPause.label = "Pause";
bPause.addEventListener(ComponentEvent.BUTTON_DOWN, pauseButton);
bPause.x = bPlay.x + bPlay.width + 10; bPause.y = bPlay.y; bPause.width = 45;
addChild(bPause);
// Stop button
var bStop : Button = new Button;
bStop.label = "Stop";
bStop.addEventListener(ComponentEvent.BUTTON_DOWN, stopButton);
bStop.x = bPause.x + bPause.width + 10; bStop.y = bPlay.y; bStop.width = 45;
addChild(bStop);
// Fullscreen button
var bFull : Button = new Button;
bFull.label = "Full";
bFull.addEventListener(ComponentEvent.BUTTON_DOWN, fullButton);
bFull.x = bStop.x + bStop.width + 10; bFull.y = bPlay.y; bFull.width = 45;
addChild(bFull);
// Replay button
var bReplay : Button = new Button;
bReplay.label = "Replay";
bReplay.addEventListener(ComponentEvent.BUTTON_DOWN, playButton);
// Add Slider and event listeners for slider
var slider : Slider = new Slider();
slider.minimum = 1;
slider.maximum = 100;
slider.move(10, 250);
slider.setSize(212, 0);
slider.addEventListener(SliderEvent.CHANGE, sliderChange);
addChild(slider);
// this will position the slider to current position of movie
var updateSlider : Timer = new Timer(100);
updateSlider.addEventListener(TimerEvent.TIMER, function(e : TimerEvent) { slider.value = stream.time * 10; } ); updateSlider.start();
// stop updating the slider if user clicks on it
slider.addEventListener(SliderEvent.THUMB_PRESS, function(e : SliderEvent) { updateSlider.stop(); } );
slider.addEventListener(SliderEvent.THUMB_RELEASE, function(e : SliderEvent) { updateSlider.start(); } );
// add play, pause and replay button to context menu
var myMenu : ContextMenu = new ContextMenu();
var item1 : ContextMenuItem = new ContextMenuItem("Replay");
var item2 : ContextMenuItem = new ContextMenuItem("Pause");
var item3 : ContextMenuItem = new ContextMenuItem("Full Screen");
myMenu.hideBuiltInItems();
myMenu.customItems.push(item1, item2, item3);
item1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, playButton);
item2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, pauseButton);
item3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, fullButton);
this.contextMenu = myMenu;
function netstat(e : NetStatusEvent) {
trace(e.info.code);
if (e.info.code == "NetStream.Play.Stop") {
}if (e.info.code == "NetStream.Play.Stop") {
bReplay.x = vid.x + (vid.width - bReplay.width) / 2;
bReplay.y = vid.y + (vid.height - bReplay.height) / 2;
addChild(bReplay);
firstRun = true; }
bReplay.y = vid.y + (vid.height - bReplay.height) / 2;
addChild(bReplay);
firstRun = true; }
function netmeta(meta : Object) {
trace(meta.duration);
if (meta.height != null && meta.width != null) {
if (meta.duration != null) slider.maximum = meta.duration * 10;
}if (meta.height != null && meta.width != null) {
trace("Video is " + meta.height + "x" + meta.width + " pixels");
vid.height = meta.height;
vid.width = meta.width;
vid.x = (stage.stageWidth - vid.width) / 2;
vid.y = vid.x;
}vid.height = meta.height;
vid.width = meta.width;
vid.x = (stage.stageWidth - vid.width) / 2;
vid.y = vid.x;
if (meta.duration != null) slider.maximum = meta.duration * 10;
function playButton(e : Event) {
if (firstRun) {
}
if (bReplay.root != null) removeChild(bReplay);
stream.play("http://www.hoomanr.com/Demos/Windmill/Windmill.flv");
firstRun = false; }
else { stream.resume(); }stream.play("http://www.hoomanr.com/Demos/Windmill/Windmill.flv");
firstRun = false; }
function pauseButton(e : Event) {
stream.togglePause();
}function stopButton(e : ComponentEvent) {
stream.pause();
stream.seek(0);
}stream.seek(0);
function sliderChange(e : SliderEvent) {
stream.seek(slider.value / 10);
if (bReplay.root != null) removeChild(bReplay);
}if (bReplay.root != null) removeChild(bReplay);
function fullButton(e : Event) {
stage.displayState = StageDisplayState.FULL_SCREEN;
}