digitalpencil
Programmer
Hi,
Can anyone please help me to understand how to go about swapping the FLV playing through the video instance on my stage.
Ordinarily just a case of updating the netstream path but am v.new to streaming video so am hoping someone with more knowledge in this area could help me out.
The following is the code i'm using to display an FLV off of my FVSS account through a standard video instance called 'player'
//
// Use this code as an example of how to use VitalStream's Content Delivery Network (CDN)
//
// Because you are using a CDN, you don't directly call netConnection.connect() or
// netStream.play(), you instead use a simple 2 step process:
//
// Step 1:
// Use the Direct-File-Link from the FVSS Project Manager (flashadmin.vitalstream.com)
// to retrieve the server the video will play from:
// vs_xml = new XML();
// vs_xml.load("// The above line needs to have your settings for: aname=YOURACCT_vitalstream_com & sname=STREAMNAME
//
// Step 2:
// The VSConnect() function is called to initiate the connection and start the netStream.
//
// The following code is a complete working example, but don't forget to change
// the aname and sname parameters in line 29 as the sample account and video used might not be
// valid in the future...
//
var m_connList:Array;
var m_serverName:String, m_appName:String, m_streamName:String, m_XMLURI:String;
var m_connListCounter:Number;
var m_flashComConnectTimeOut;
//place direct file link here (from the VitalStream Flash Project Manager)
// aname is the application
// sname is the name of the stream (video)
//m_XMLURI = "m_XMLURI = "
//this is the list of protocols that we check for the fastest connection
// put in the order that you want checked
m_connList = [
{protocol:"rtmp", port:1935},
{protocol:"rtmp", port:443},
{protocol:"rtmpt", port:80}
];
//The direct link returns an XML file containing the application name, stream
// name, and the server address. The code below parses this information from
// the XML file.
vs_xml = new XML();
vs_xml.load(m_XMLURI);
vs_xml.onLoad = function(success) {
if (success) {
appURL = this.firstChild.firstChild.nextSibling.firstChild.nodeValue;
var appURLArray:Array = appURL.split('/');
serverName = appURLArray[2];
//this will rebuild the application name from the array
for (iAN = 3; iAN < appURLArray.length; iAN++) {
appName += appURLArray[iAN] + '/';
}
appName = appName.substring(0, appName.length-1)
streamName = this.firstChild.firstChild.firstChild.nodeValue;
//Flashcom does not like the .flv extension on the file, this will detect
// if it is there and remove it if needed.
if (streamName.length > 4) {
if (streamName.substr(streamName.length-4, 4).toLowerCase() == ".flv") {
streamName = streamName.substr(0, streamName.length-4)
}
}
trace("serverName : " + serverName);
trace("appName : " + appName);
trace("streamName : " + streamName);
//We have the stream, app and server, call the connect function
VSConnect(serverName, appName, false, streamName);
} else {
//handle error
trace("Loading Error!");
}
};
//this function is to set the root varibles for the app, stream, server, and for auto
// bandwidth detection (not currently being used, will always be false)
function VSConnect(serverName:String, appName:String, autoSenceBW:Boolean, streamName:String):Void {
m_appName = appName;
m_autoSenseBW = autoSenceBW;
m_streamName = streamName;
m_serverName = serverName;
trace("connect: " + m_serverName);
//This will remove the timeout timer if connections were already being made. starting anew.
clearInterval(m_flashComConnectTimeOut);
m_flashComConnectTimeOut = null;
delete m_flashComConnectTimeOut;
//this will set the the timeout to 60 seconds, if no connections succeed it will close any
// pending connections after one minute.
m_flashComConnectTimeOut = setInterval(this, "onFlashComConnectTimeOut", 60 * 1000);
//this will kick off the actual connection to the server now that it is setup.
actualConnect();
}
function actualConnect() {
//this will create a netConnection for every one of the protocols/ports listed in
// the m_connList, the first connection to succeed will be used, all others will
// be closed.
for (var i = 0; i < m_connList.length; i++) {
this["nc" + i] = new NetConnection();
this["nc" + i].mc = this;
this["nc" + i].connIndex = i;
//the server calls this object to continue (auto bandwidth detection is complete)
this["nc" + i].onBWDone = function(p_bw) {
trace("onBWDone: " + p_bw);
//we pass the successful netConnection and the calculated bandwidth speed to
// the onconnected function to continue
this.mc.onConnected(this, p_bw);
};
m_payload = 0;
//this function is not currently used; it is for checking bandwidth speed
// initiated by the server
this["nc" + i].onBWCheck = function() {
trace("onBWCheck returning: " + (this.mc.m_payload + 1));
return ++this.mc.m_payload;
};
//this will print all netConnection status messages and kill the pending connections
// on a connection success
this["nc" + i].onStatus = function(info) {
this.pending = false;
if (info.code == "NetConnection.Connect.Success") {
trace("Connection " + this.mc.m_connList[this.connIndex].protocol + ":" + this.mc.m_connList[this.connIndex].port + " succeeded!");
for (var i = 0; i < this.mc.m_connList.length; i++) {
if (i == this.connIndex) {
continue;
}
if (this.mc["nc" + i].pending) {
clearInterval(this.mc["ncInt" + i]);
this.mc["nc" + i].onStatus = null;
this.mc["nc" + i].close();
this.mc["nc" + i] = null;
delete this.mc["nc" + i];
}
}
} else {
trace(this.mc.m_connList[this.connIndex].protocol + ": " + this.mc.m_connList[this.connIndex].port + ", onStatus: " + info.code + " : " + info.description);
}
};
this["nc" + i].pending = true;
}
m_connListCounter = 0;
//this will initiate the connection;
// the proper connection scheme for a player is .connect(appURI, false, streamName)
// for a publisher it should be .connect(appURI, false, streamName, "publisher")
this["nc" + m_connListCounter].connect(m_connList[m_connListCounter].protocol + "://" + m_serverName + ":" + m_connList[m_connListCounter].port + "/" + m_appName, m_autoSenseBW, m_streamName);
// if we have not reached the end of the connection list then try the next in the list.
if (m_connListCounter<(m_connList.length-1)) {
m_connListCounter++;
this["ncInt"+m_connListCounter] = setInterval(this, "nextConnect", 1500);
}
}
//This function will walk through the rest of the connection list to try every protocol.
function nextConnect() {
clearInterval(this["ncInt"+m_connListCounter]);
this["nc"+m_connListCounter].connect(m_connList[m_connListCounter].protocol+"://"+m_serverName+":"+m_connList[m_connListCounter].port+"/"+m_appName, m_autoSenseBW, m_streamName);
if (m_connListCounter<(m_connList.length-1)) {
m_connListCounter++;
this["ncInt"+m_connListCounter] = setInterval(this, "nextConnect", 1500);
}
}
//once we are connected to the server this function will remove
// time out function and extraneous properties used for initiating
// the connection.
function onConnected(p_nc:NetConnection, p_bw:Number):Void {
trace("onConnected called");
m_mainNC = p_nc;
clearInterval(m_flashComConnectTimeOut);
m_flashComConnectTimeOut = null;
delete m_flashComConnectTimeOut;
delete m_mainNC.onBWDone;
delete m_mainNC.onBWCheck;
m_mainNC.mc = this;
m_theURI = m_mainNC.uri;
//calls function with the netConnection and the BandWidth ammount
startStream(p_nc, p_bw);
}
//this will start the stream (video) playing
function startStream(p_nc, p_bw) {
ns = new NetStream(p_nc);
//this will attacth the video to the video object.
// in this instance the video object is named player, change
// this to match whatever you are using. The Video Object is
// inserted into the frame from the library.
player.attachVideo(ns);
ns.play(m_streamName);
//this will print all netstream status information that the player
// recieves
ns.onStatus = function(info) {
trace(info.code + " : " + info.description);
};
}
//this will kill all pending netConnections in the event that none succeed
// by the end of the time interval
function onFlashComConnectTimeOut() {
trace("all connections failed, quitting");
clearInterval(m_flashComConnectTimeOut);
for (var i = 0; i < m_connList.length; i++) {
if (this.mc["nc" + i].pending) {
clearInterval(this.mc["ncInt" + i]);
this.mc["nc" + i].onStatus = null;
this.mc["nc" + i].close();
this.mc["nc" + i] = null;
delete this.mc["nc" + i];
}
}
}
Seems somewhat long-winded to me, but as previously mentioned, i've only previously had experience with progressive download so perhaps this is the only way to do this.
All i'm after is just a simple way to update or swap the video playing by changing the FLV path.
I imagine this is fairly simple i'm just lost with all this streaming stuff, so If anyone has any ideas how to go about it, i'd be greatly appreciative to hear their thoughts.
Thanks in advance
Can anyone please help me to understand how to go about swapping the FLV playing through the video instance on my stage.
Ordinarily just a case of updating the netstream path but am v.new to streaming video so am hoping someone with more knowledge in this area could help me out.
The following is the code i'm using to display an FLV off of my FVSS account through a standard video instance called 'player'
//
// Use this code as an example of how to use VitalStream's Content Delivery Network (CDN)
//
// Because you are using a CDN, you don't directly call netConnection.connect() or
// netStream.play(), you instead use a simple 2 step process:
//
// Step 1:
// Use the Direct-File-Link from the FVSS Project Manager (flashadmin.vitalstream.com)
// to retrieve the server the video will play from:
// vs_xml = new XML();
// vs_xml.load("// The above line needs to have your settings for: aname=YOURACCT_vitalstream_com & sname=STREAMNAME
//
// Step 2:
// The VSConnect() function is called to initiate the connection and start the netStream.
//
// The following code is a complete working example, but don't forget to change
// the aname and sname parameters in line 29 as the sample account and video used might not be
// valid in the future...
//
var m_connList:Array;
var m_serverName:String, m_appName:String, m_streamName:String, m_XMLURI:String;
var m_connListCounter:Number;
var m_flashComConnectTimeOut;
//place direct file link here (from the VitalStream Flash Project Manager)
// aname is the application
// sname is the name of the stream (video)
//m_XMLURI = "m_XMLURI = "
//this is the list of protocols that we check for the fastest connection
// put in the order that you want checked
m_connList = [
{protocol:"rtmp", port:1935},
{protocol:"rtmp", port:443},
{protocol:"rtmpt", port:80}
];
//The direct link returns an XML file containing the application name, stream
// name, and the server address. The code below parses this information from
// the XML file.
vs_xml = new XML();
vs_xml.load(m_XMLURI);
vs_xml.onLoad = function(success) {
if (success) {
appURL = this.firstChild.firstChild.nextSibling.firstChild.nodeValue;
var appURLArray:Array = appURL.split('/');
serverName = appURLArray[2];
//this will rebuild the application name from the array
for (iAN = 3; iAN < appURLArray.length; iAN++) {
appName += appURLArray[iAN] + '/';
}
appName = appName.substring(0, appName.length-1)
streamName = this.firstChild.firstChild.firstChild.nodeValue;
//Flashcom does not like the .flv extension on the file, this will detect
// if it is there and remove it if needed.
if (streamName.length > 4) {
if (streamName.substr(streamName.length-4, 4).toLowerCase() == ".flv") {
streamName = streamName.substr(0, streamName.length-4)
}
}
trace("serverName : " + serverName);
trace("appName : " + appName);
trace("streamName : " + streamName);
//We have the stream, app and server, call the connect function
VSConnect(serverName, appName, false, streamName);
} else {
//handle error
trace("Loading Error!");
}
};
//this function is to set the root varibles for the app, stream, server, and for auto
// bandwidth detection (not currently being used, will always be false)
function VSConnect(serverName:String, appName:String, autoSenceBW:Boolean, streamName:String):Void {
m_appName = appName;
m_autoSenseBW = autoSenceBW;
m_streamName = streamName;
m_serverName = serverName;
trace("connect: " + m_serverName);
//This will remove the timeout timer if connections were already being made. starting anew.
clearInterval(m_flashComConnectTimeOut);
m_flashComConnectTimeOut = null;
delete m_flashComConnectTimeOut;
//this will set the the timeout to 60 seconds, if no connections succeed it will close any
// pending connections after one minute.
m_flashComConnectTimeOut = setInterval(this, "onFlashComConnectTimeOut", 60 * 1000);
//this will kick off the actual connection to the server now that it is setup.
actualConnect();
}
function actualConnect() {
//this will create a netConnection for every one of the protocols/ports listed in
// the m_connList, the first connection to succeed will be used, all others will
// be closed.
for (var i = 0; i < m_connList.length; i++) {
this["nc" + i] = new NetConnection();
this["nc" + i].mc = this;
this["nc" + i].connIndex = i;
//the server calls this object to continue (auto bandwidth detection is complete)
this["nc" + i].onBWDone = function(p_bw) {
trace("onBWDone: " + p_bw);
//we pass the successful netConnection and the calculated bandwidth speed to
// the onconnected function to continue
this.mc.onConnected(this, p_bw);
};
m_payload = 0;
//this function is not currently used; it is for checking bandwidth speed
// initiated by the server
this["nc" + i].onBWCheck = function() {
trace("onBWCheck returning: " + (this.mc.m_payload + 1));
return ++this.mc.m_payload;
};
//this will print all netConnection status messages and kill the pending connections
// on a connection success
this["nc" + i].onStatus = function(info) {
this.pending = false;
if (info.code == "NetConnection.Connect.Success") {
trace("Connection " + this.mc.m_connList[this.connIndex].protocol + ":" + this.mc.m_connList[this.connIndex].port + " succeeded!");
for (var i = 0; i < this.mc.m_connList.length; i++) {
if (i == this.connIndex) {
continue;
}
if (this.mc["nc" + i].pending) {
clearInterval(this.mc["ncInt" + i]);
this.mc["nc" + i].onStatus = null;
this.mc["nc" + i].close();
this.mc["nc" + i] = null;
delete this.mc["nc" + i];
}
}
} else {
trace(this.mc.m_connList[this.connIndex].protocol + ": " + this.mc.m_connList[this.connIndex].port + ", onStatus: " + info.code + " : " + info.description);
}
};
this["nc" + i].pending = true;
}
m_connListCounter = 0;
//this will initiate the connection;
// the proper connection scheme for a player is .connect(appURI, false, streamName)
// for a publisher it should be .connect(appURI, false, streamName, "publisher")
this["nc" + m_connListCounter].connect(m_connList[m_connListCounter].protocol + "://" + m_serverName + ":" + m_connList[m_connListCounter].port + "/" + m_appName, m_autoSenseBW, m_streamName);
// if we have not reached the end of the connection list then try the next in the list.
if (m_connListCounter<(m_connList.length-1)) {
m_connListCounter++;
this["ncInt"+m_connListCounter] = setInterval(this, "nextConnect", 1500);
}
}
//This function will walk through the rest of the connection list to try every protocol.
function nextConnect() {
clearInterval(this["ncInt"+m_connListCounter]);
this["nc"+m_connListCounter].connect(m_connList[m_connListCounter].protocol+"://"+m_serverName+":"+m_connList[m_connListCounter].port+"/"+m_appName, m_autoSenseBW, m_streamName);
if (m_connListCounter<(m_connList.length-1)) {
m_connListCounter++;
this["ncInt"+m_connListCounter] = setInterval(this, "nextConnect", 1500);
}
}
//once we are connected to the server this function will remove
// time out function and extraneous properties used for initiating
// the connection.
function onConnected(p_nc:NetConnection, p_bw:Number):Void {
trace("onConnected called");
m_mainNC = p_nc;
clearInterval(m_flashComConnectTimeOut);
m_flashComConnectTimeOut = null;
delete m_flashComConnectTimeOut;
delete m_mainNC.onBWDone;
delete m_mainNC.onBWCheck;
m_mainNC.mc = this;
m_theURI = m_mainNC.uri;
//calls function with the netConnection and the BandWidth ammount
startStream(p_nc, p_bw);
}
//this will start the stream (video) playing
function startStream(p_nc, p_bw) {
ns = new NetStream(p_nc);
//this will attacth the video to the video object.
// in this instance the video object is named player, change
// this to match whatever you are using. The Video Object is
// inserted into the frame from the library.
player.attachVideo(ns);
ns.play(m_streamName);
//this will print all netstream status information that the player
// recieves
ns.onStatus = function(info) {
trace(info.code + " : " + info.description);
};
}
//this will kill all pending netConnections in the event that none succeed
// by the end of the time interval
function onFlashComConnectTimeOut() {
trace("all connections failed, quitting");
clearInterval(m_flashComConnectTimeOut);
for (var i = 0; i < m_connList.length; i++) {
if (this.mc["nc" + i].pending) {
clearInterval(this.mc["ncInt" + i]);
this.mc["nc" + i].onStatus = null;
this.mc["nc" + i].close();
this.mc["nc" + i] = null;
delete this.mc["nc" + i];
}
}
}
Seems somewhat long-winded to me, but as previously mentioned, i've only previously had experience with progressive download so perhaps this is the only way to do this.
All i'm after is just a simple way to update or swap the video playing by changing the FLV path.
I imagine this is fairly simple i'm just lost with all this streaming stuff, so If anyone has any ideas how to go about it, i'd be greatly appreciative to hear their thoughts.
Thanks in advance