topical media & game development
mashup-rmx-10-VideoPlayer-videoPlayer.ax
mashup-rmx-10-VideoPlayer-videoPlayer.ax
[swf]
flex
// ActionScript file videoPlayer.as
import(s)
import mx.events.VideoEvent;
import mx.formatters.DateFormatter;
import flash.events.MouseEvent;
import mx.rpc.http.mxml.HTTPService;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private variable(s)
private var videoLength:String;
private var start:Date;
private var timeDisplayFormatter:DateFormatter;
private var seekTo:Number;
private var playlist:XMLList;
private var playlistCursor:uint;
bindable(s)
[Bindable]
private var videoFileTotalBytes:Number;
This method is fired on the creationComplete event of the VideoPlayer application.
init(s)
private function init():void
{
start = new Date("1/1/2000");
timeDisplayFormatter = new DateFormatter();
this.myVideoDisplay.addEventListener(VideoEvent.READY, videoReady);
this.myVideoDisplay.addEventListener(VideoEvent.PLAYHEAD_UPDATE, updateTimeDisplay);
this.myVideoDisplay.addEventListener(VideoEvent.COMPLETE, videoComplete);
this.btn_next.addEventListener(MouseEvent.CLICK, playlistControlsHandler);
this.btn_previous.addEventListener(MouseEvent.CLICK, playlistControlsHandler);
this.btn_playToggle.addEventListener(MouseEvent.CLICK, togglePlayback);
this.btn_stop.addEventListener(MouseEvent.CLICK, stopPlayback);
loadPlaylist();
}
This method loads the playlist xml file.
load playlist
private function loadPlaylist():void
{
playlistCursor = 0;
var playlistService:HTTPService = new HTTPService();
// playlistService.url = "mashup-rmx-10-VideoPlayer-playlist.xml";
playlistService.url = playlisturl; // because of clash with url and playlist
playlistService.resultFormat = "xml";
playlistService.showBusyCursor = true;
playlistService.addEventListener(ResultEvent.RESULT, onPlaylistResult);
playlistService.addEventListener(FaultEvent.FAULT, onFault);
playlistService.send();
}
This method is fired when the a successful result is received by the
HTTPService object.
result(s)
private function onPlaylistResult(event:ResultEvent):void
{
trace(event.result);
var resultXML:XML = new XML(event.result);
playlist = new XMLList(resultXML.video);
playVideo();
}
This method plays a video based on the playlistCursor variable.
play video(s)
private function playVideo():void
{
if (this.playlist[playlistCursor].@type == 'ad')
{
this.toggleVideoControls(false);
}
else
{
this.toggleVideoControls(true);
} // literally @<em></em>file
this.myVideoDisplay.source = this.playlist[playlistCursor].@ax-mashup-rmx-10-VideoPlayer-videoPlayer.toString();
this.myVideoDisplay.load();
}
Fired when there is an error loading the playlist xml.
load(s) / fault(s)
private function onFault(event:FaultEvent):void
{
trace(event.fault);
}
This method is fired when a VideoEvent.READY is dispatched by the VideoDisplay
ready
private function videoReady(event:VideoEvent):void
{
timeDisplayFormatter.formatString = "NN:SS";
var totalTime:Date = new Date ( start.getTime() + (this.myVideoDisplay.totalTime * 1000) );
this.videoLength = timeDisplayFormatter.format(totalTime);
}
This method is fired when there is a VideoEvent.PLAYHEAD_UPDATE event is dispatched
by the VideoDisplay
update(s)
private function updateTimeDisplay(event:VideoEvent):void
{
timeDisplayFormatter.formatString = "N:SS";
var currentTime:Date = new Date ( start.getTime() + (event.playheadTime * 1000) );
this.tf_playtimeDisplay.text = timeDisplayFormatter.format(currentTime) + "/" + this.videoLength;
}
This method toggles the pause/play state.
pause/play toggle
private function togglePlayback(event:MouseEvent):void
{
if (this.myVideoDisplay.playing)
{
this.myVideoDisplay.pause();
}
else if (this.myVideoDisplay.source)
{
this.myVideoDisplay.play();
}
}
This method stops playback on the VideoDisplay
stop button
private function stopPlayback(event:MouseEvent):void
{
this.myVideoDisplay.stop();
}
This method is fired when the VideoDisplay reaches the end of
a video and dispatches a VideoEvent.COMPLETE event.
video complete (event)
private function videoComplete(event:VideoEvent):void
{
if (this.playlistCursor < this.playlist.length() - 1)
{
this.myVideoDisplay.playheadTime = 0;
this.playlistCursor++;
this.playVideo();
}
}
This method handles clicks from the Next and Prev buttons.
next/previous button(s)
private function playlistControlsHandler(event:MouseEvent):void
{
switch (event.currentTarget.label)
{
case 'Next':
if (this.playlistCursor < this.playlist.length() - 1)
{
if (this.myVideoDisplay.playing) { this.myVideoDisplay.pause(); }
this.myVideoDisplay.playheadTime = 0;
this.playlistCursor++;
this.playVideo();
}
break;
prev
case 'Prev':
if (this.playlistCursor - 1 >= 0)
{
if (this.myVideoDisplay.playing) { this.myVideoDisplay.pause(); }
this.myVideoDisplay.playheadTime = 0;
this.playlistCursor--;
this.playVideo();
}
break;
default :
break;
}
}
This method enables/disables the player controls.
enable/disable toggle(s)
private function toggleVideoControls(enable:Boolean):void
{
this.btn_playToggle.enabled = enable;
this.btn_next.enabled = enable;
this.btn_previous.enabled = enable;
this.btn_stop.enabled = enable;
this.playbackProgress.enabled = enable;
}
(C) Æliens
04/09/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.