Trouble adding video controls to video selected by XML comboBox
I'm much closer to my final desired outcome, thank you.
Two remaining issues, when I hit previous and next, the name shown in the combobox sometimes gets out of sync.
Also I can't seem to get the video scrubber to work.
See it in action
add index.zip to the link above to dl source files (don't have enough points to post 2 links)
import fl.data.DataProvider;
import flash.net.NetStream;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var videosXML:XML = new XML();
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("xml/videos.xml");
var videos:Array = new Array({label:"Select a Video",data:""});
var client:Object = new Object();
var _currentVideoId:int = 0;
var _isPlaying:Boolean = false;
var stream:NetStream;
var videoURL:String;
var connection:NetConnection;
var videoInterval = setInterval(videoStatus,100);
var amountLoaded:Number;
var meta:Object = new Object();
var duration:Number;
// load the XML
loader.addEventListener(Event.COMPLETE,loaderOnComplete);
loader.load(request);
function loaderOnComplete(event:Event):void
{
videosXML = new XML(event.target.data);
for each (var video:XML in videosXML.video)
{
videos.push({label:video.name.toString(),data:video.url.toString()});
}
moviesCB.dataProvider = new DataProvider(videos);
// load the first video
initialize_video_player();
}
function initialize_video_player():void
{
// loads first video in list
videoURL = videosXML.video[0].url;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.connect(null);
moviesCB.selectedIndex = _currentVideoId++;
}
function playerControls():void
{
pauseBtn.addEventListener(MouseEvent.CLICK, PauseVideo, false, 0, true);
playBtn.addEventListener(MouseEvent.CLICK, PlayVideo, false, 0, true);
nextBtn.addEventListener(MouseEvent开发者_JS百科.CLICK, NextVideo);
previousBtn.addEventListener(MouseEvent.CLICK, PreviousVideo);
}
function PauseVideo($e:MouseEvent):void
{
stream.togglePause();
}
function PlayVideo($e:MouseEvent):void
{
stream.resume();
}
function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
trace("Unable to locate video: " + videoURL);
break;
}
}
function connectStream():void
{
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
theVideo.attachNetStream(stream);
playerControls();
}
function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore AsyncErrorEvent events.
}
moviesCB.addEventListener(Event.CHANGE, changeHandler);
function changeHandler(event:Event):void
{
if (ComboBox(event.target).selectedItem.data != "")
{
stream.play(ComboBox(event.target).selectedItem.data);
_currentVideoId=moviesCB.selectedIndex;
}
}
function NextVideo(e:MouseEvent):void
{
if (_currentVideoId<videosXML.video.length()-1)
{
_currentVideoId++;
loadNewVideo();
}
else
{
// do nothing, already at maximum videoId
}
}
function PreviousVideo(e:MouseEvent):void
{
trace ("previous video with current video of "+_currentVideoId);
if (_currentVideoId>0)
{
_currentVideoId--;
loadNewVideo();
}
else
{
// do nothing, already at 0
}
}
function loadNewVideo():void
{
// because it's not a mouse action, tell the stream what to play
stream.play(videosXML.video[_currentVideoId].url.toString());
// and update the selectedIndex of the ComboBox
moviesCB.selectedIndex=_currentVideoId;
}
function videoStatus()
{
amountLoaded = ns.bytesLoaded / ns.bytesTotal;
videoLoader.loadBar._width = amountLoaded * 640;
}
meta.onMetaData = function(meta:Object)
{
trace(meta.duration);
}
ns.client = meta;
/////// Original Post //////////////
Hello, it's been a few years since I've touched flash, so perhaps I'm just overlooking something. If anyone could look at the code and offer any suggestions that would be awesome.
What's working, I select a video from a combobox that is populated from an XML file, pick the video and it plays.
I've been trying to add pause/play, stop, forward and reverse functionality, once I get that to work I also want to add a video scrubber(slider), and previous/next buttons to go to the previous/next video as listed in the xml file.
At the moment I have a component button on the stage called playButton, which I'm trying to use for pause/play functionality. Below is my code, the player control is at the very bottom. Thanks.
import fl.data.DataProvider;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var videosXML:XML = new XML();
var loader:URLLoader = new URLLoader();
var request:URLRequest= new URLRequest("xml/videos.xml");
var videos:Array = new Array({label:"Select a Video",data:""});
var client:Object = new Object();
theVideo.attachNetStream(ns);
ns.client = client;
loader.addEventListener(Event.COMPLETE,loaderOnComplete);
loader.load (request);
function loaderOnComplete(event:Event):void{
videosXML = new XML(event.target.data);
for each (var video:XML in videosXML.video){
videos.push({label:video.name.toString(),data:video.url.toString()});
}
moviesCB.dataProvider = new DataProvider(videos);
}
moviesCB.addEventListener(Event.CHANGE, changeHandler);
function changeHandler(event:Event):void {
if(ComboBox(event.target).selectedItem.data != ""){
ns.play(ComboBox(event.target).selectedItem.data);
}
};
client.onMetaData = metadataHandler;
function metadataHandler(md:Object):void{
}
//player controls
playButton.onRelease = function() {
ns.pause();
}
I think you want to do this (Click Handlers are different in AS3)
playButton.addEventListener(MouseEvent.CLICK, onTogglePlay);
function onTogglePlay(e:MouseEvent):void {
ns.togglePause();
}
You should really put a listener on the NC for the NetStatusEvent and then create the NetStream once the connection is connected.
The answer to my original question was to use stream.bytesTotal and stream.bytesLoaded instead of ns.bytesTotal and ns.bytesLoaded
精彩评论