package com.clementegomez.video
/*****************DISCLAIMER*************************************************
** *
**Title: VideoViewer *
**Function: Screen Portion of a VideoPlayer. *
**Can be used standalone or with the ProgressionBar Class. *
*** *
** *
**Features *
**_____________________________ *
* * Plays FLV's *
* * Error Checking w/ Text Field Messages *
* * Integratable with ProgressionBar Class *
* * Small File Size *
* * Playlist Feature (XML) *
** *
** *
** *
**Author: Clemente Gomez *
**Email: zomegpro@gmail.com *
**Link: blog.kreativeking.com *
**Version: 2.0 *
**Date: July 30 2008 *
** *
**License: GNU GPL *
** *
**This class can be used in anyway that the user feels fit. *
**Keeping this Disclaimer as credit to the authoer is desired but not *
**necessary *
*****************************************************************************/
{
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.events.Event;
import flash.media.Video;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.media.SoundTransform;
import com.clementegomez.utils.XMLLoader;
import com.clementegomez.events.VVInfoEvent;
import com.clementegomez.events.DurationEvent;
import com.clementegomez.events.ParamEvent;
import com.clementegomez.media.MediaTimer;
public class VideoViewer extends Sprite
{
private var _ns:NetStream;
private var _buffer:Number;
private var toPlay:String;
private var video:String;
private var _h:Number;
private var _w:Number;
private var _duration:Number;
private var delayedCall:Timer;
private var _seekPos:Number;
private var _VideoViewer:VideoViewer;
private var _videoList:XMLList;
private var _videoSettings:XMLList;
private var _useList:Boolean;
private var _videoPos:int;
private var _videoDelay:Timer;
private var _reReady:Boolean;
private var _updatedTime:Number;
private var _mTimer:MediaTimer;
private var _title:String;
private var nClient:Object;
private var _eventTimer:Timer;
private var myVid:Video;
public function VideoViewer()
{
var nc:NetConnection;
var ratio:Number;
_mTimer = new MediaTimer();
_VideoViewer = this;
_reReady = false;
_videoPos = 0;
_useList = false;
delayedCall = new Timer(.001, 0);
_eventTimer = new Timer(10);
_eventTimer.addEventListener(TimerEvent.TIMER, dispatchDuration);
delayedCall.addEventListener(TimerEvent.TIMER, VVInfo);
nc = new NetConnection();
nc.connect(null);
nClient = new Object();
_ns = new NetStream(nc);
myVid = new Video();
addChild(myVid);
myVid.x = 0;
myVid.y = 0;
myVid.height = 0;
myVid.width = 0;
myVid.height = _VideoViewer.height;
myVid.width = _VideoViewer.width;
myVid.attachNetStream(_ns);
_ns.client = _VideoViewer;
//_ns.bufferTime = _buffer;
delayedCall.start();
_ns.addEventListener(NetStatusEvent.NET_STATUS, netStat);
}
public function onMetaData(info:Object)
{
nClient = info;
_eventTimer.start();
}
private function dispatchDuration(e:TimerEvent)
{
_duration = nClient.duration;
dispatchEvent(new DurationEvent(_duration));
}
private function makeVideoList(e:Event)
{
var list:XMLList = e.target.getList[0];
var settings:XMLList = e.target.getList[1];
_videoList = list;
_videoSettings = settings;
toPlay = _videoList[_videoPos];
readyVideo();
returnList();
returnSettings();
dispatchEvent(new ParamEvent(ParamEvent.XML_LOADED));
function returnList()
{
return _videoList;
}
function returnSettings()
{
return _videoSettings;
}
}
private function netStat(e:NetStatusEvent)
{
if(e.info.code == "NetStream.Play.Stop")
{
if(_ns.time >= _duration)
{
completed();
}
}
if(e.info.code == "NetStream.Play.StreamNotFound")
{
var errorField:TextField = new TextField();
var displayTimer:Timer = new Timer(5000, 0);
displayTimer.addEventListener(TimerEvent.TIMER, removeError);
errorField.text = "Video Not Found : Please Check to see that the file property is set and that the spelling is correct";
errorField.width = this.width / 2;
errorField.x = _VideoViewer.width / 4;
errorField.y = _VideoViewer.height / 4;
errorField.wordWrap = true;
errorField.multiline = true;
this.addChild(errorField);
displayTimer.start();
trace("Video Not Found");
trace("Please check to see that the file set exists and that the spelling is correct");
function removeError(e:TimerEvent)
{
displayTimer.stop();
_VideoViewer.removeChild(errorField);
displayTimer.removeEventListener(TimerEvent.TIMER, removeError);
}
}
}
private function VVInfo(e:TimerEvent)
{
delayedCall.stop();
delayedCall.removeEventListener(TimerEvent.TIMER, VVInfo);
dispatchEvent(new VVInfoEvent(this, this.height, this.width));
}
private function getVideo()
{
return toPlay;
}
private function playVideo()
{
video = getVideo();
_ns.play(video);
}
private function completed()
{
if (!_useList || _videoPos == _videoList.length() - 1)
{
_reReady = true;
removeEventListener(Event.ENTER_FRAME, mediaTimer);
dispatchEvent(new Event(Event.COMPLETE));
}
else
{
removeEventListener(Event.ENTER_FRAME, mediaTimer);
_VideoViewer.stopVideo();
_videoDelay = new Timer(_videoSettings[0] * 1000, 0);
_videoDelay.addEventListener(TimerEvent.TIMER, nextVideo);
_videoPos = _videoPos + 1;
toPlay = _videoList[_videoPos];
_buffer = _videoList[_videoPos].attribute("buffer");
_VideoViewer.height = _videoList[_videoPos].attribute("height");
_VideoViewer.width = _videoList[_videoPos].attribute("width");
dispatchEvent(new ParamEvent(ParamEvent.COMPLETE, _VideoViewer.width));
_videoDelay.start();
function nextVideo(e:TimerEvent)
{
_videoDelay.stop();
readyVideo();
toggleVideo();
}
}
}
private function mediaTimer(e:Event)
{
_mTimer.time = _ns.time;
_mTimer.duration = _duration;
_updatedTime = _ns.time;
_mTimer.dispatchEvent(new Event(Event.CHANGE));
return _updatedTime;
}
//Sets Up Video to be played. Must be called before playing any video.
public function readyVideo()
{
addEventListener(Event.ENTER_FRAME, mediaTimer);
_reReady = false;
_VideoViewer.playVideo();
_VideoViewer.toggleVideo();
}
//Use to go to a specific frame in the Video.'time' in the constructor needs to be the frame of the video you want to seek to.
public function seeker(n:Number)
{
_ns.seek(n);
}
//Toggles the video to play and pause. When readyVideo() is called first, this function plays the video. When called again it pauses.
public function toggleVideo()
{
if (_VideoViewer.file == null)
{
var errorField:TextField = new TextField();
var displayTimer:Timer = new Timer(5000, 0);
displayTimer.addEventListener(TimerEvent.TIMER, removeError);
errorField.text = "Video Not Set : The file property was not set. Please set what movie you would like to play.";
errorField.width = this.width / 2;
errorField.x = _VideoViewer.width / 4;
errorField.y = _VideoViewer.height / 4;
errorField.wordWrap = true;
errorField.multiline = true;
this.addChild(errorField);
displayTimer.start();
trace("Video Not Set");
trace("The file property was not set. Please set what movie you would like to play.");
function removeError(e:TimerEvent)
{
displayTimer.stop();
_VideoViewer.removeChild(errorField);
displayTimer.removeEventListener(TimerEvent.TIMER, removeError);
}
}
else
{
if (_videoPos == 0)
{
trace();
dispatchEvent(new ParamEvent(ParamEvent.COMPLETE, _VideoViewer.width));
}
_ns.togglePause();
}
}
//Stops the video and unloads the clip that was loaded into the Viewer.
public function stopVideo()
{
_ns.close();
}
public function loadVideoList()
{
_useList = true;
var videoXML:XMLLoader = new XMLLoader(["videos.xml", "config.xml"]);
videoXML.addEventListener(Event.COMPLETE, makeVideoList);
}
public function set ns(stream:NetStream)
{
_ns = stream;
}
public function get ns()
{
return _ns;
}
public function get thisVideo()
{
return myVid;
}
public function set file(video:String)
{
toPlay = video;
}
public function get file()
{
return toPlay;
}
public function set buffer(value:Number)
{
_buffer = value;
}
public function get buffer()
{
return _buffer;
}
public function get barOffset()
{
return _videoSettings[1];
}
public function get videoOffset()
{
return _videoSettings[2];
}
public function get title()
{
return _videoList[_videoPos].attribute("title");
}
public function get still()
{
return _videoList[_videoPos].attribute("still");
}
public function get ready()
{
return _reReady;
}
public function get timer()
{
return _mTimer;
}
public function get soundTrans()
{
return _ns.soundTransform;
}
public function set soundTrans($value:SoundTransform)
{
_ns.soundTransform = $value;
}
}
}
Monday, September 1, 2008
New VideoViewer Class Source
Alot of people were asking me to post the source for my classes as I post them. So here is the updated source.
Subscribe to:
Post Comments (Atom)
[...] Read more [...]
ReplyDelete