dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Flash 8 AS 2 Issue with XML Driven Player

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    10

    resolved [RESOLVED] Flash 8 AS 2 Issue with XML Driven Player

    Thanks in advance from someone who has been probably walking around the answer for about three hours now.

    I have a flash file with two layers. One for actions, the other the video layer, not the component.

    The code here is for a playlist of videos delivered via xml. This flash file I have will play the first video, but then stops. I know the answer is probably so simple, I'm going to get laughed at. That's ok, so long as I get peace from this demon.

    p.s. - Please don't bother lambasting me on my horrid skills in Action Script. This is the third week into learning Flash at all. You were once where I am now, whether you would like to admit it or not...

    Code follows, and thank you again in advance.

    Code:
    stop();
    // global objects....
    
    // playlist object for the playlist from raw xml data
    	var playlistData:Object = new Object();
    	playlistData.list = new Array();
    	
    // Create a videos object to hold a video	
    	var videos:Object = new Object();
    	
    // create variable for keeping count of videos played
    	var hasPlayed:Object = new Object();
    
    // create variable for total movies to be played
    	var quantity:Object = new Object();
    
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		total = xmlNode.childNodes.length;
    		for (i=0; i<total; i++) {
    			playlistData.list[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    			descriptions.list[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    		}
    		trace ("Loaded "+total+" entries from XML source.");
    	} else {
    		content = "file not loaded!";
    	}
    	trace("Trying to start vids...");
    	startVids();
    }
    
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    var myChannel:String = _level0.channel;
    var myEpisode:String = _level0.episode;
    var myXmlFile:String = "http://www.yourdomain.com/my.xml";
    xmlData.load(myXmlFile);
    
    function startVids() {
    	trace("Made it to start vids. Making a new connection...");
    // Create a NetConnection object:
    	var nc:NetConnection = new NetConnection();
    	trace("Net connection to null...");
    // Create a local streaming connection:
    	nc.connect(null);
    	trace("New netstream connection...");
    // Create a NetStream object and define an onStatus() function:
    	var ns:NetStream = new NetStream(nc);
    	trace("Set the time to buffer");
    // Set the buffer time:
    	ns.setBufferTime(5);
    	trace("Attching netstream to instance of video");
    // Attach the NetStream video feed to the Video object:
    	theVideo.attachVideo(ns);
    
    	theVideo.addEventListener("complete",nsListener);
    	
    	ns.onStatus = function(info) {
    		if(info.code == "NetStream.Buffer.Full") {
    			buffer._visible = false;
    		}
    		if(info.code == "NetStream.Buffer.Empty") {
    			buffer._visible = true;
    		}
    		if(info.code == "NetStream.Play.Stop") {
    			if(hasplayed == quantity) {
    				trace("Time to close because "+hasplayed+" equals "+quantity);
    				ns.close();
    			} else {
    				myInt=setInterval(loadNext,1000);
    			}
    		}
    	}
    
    trace("Play the first video in the lineup, which is "+playlistData.list[0]);
    // Being playing the FLV file:
    ns.play(playlistData.list[0]);
    	trace("Set hasplayed to 1");
    hasplayed = 1;
    	trace("Get quantity, which is "+playlistData.list.length);
    quantity = playlistData.list.length;
    
    nsListener.complete = function( evt:Object ):Void {
        loadNext();
    };
    
    
    }
    
    function loadNext() {
    	if(hasplayed < quantity) {
    	    clearInterval(myInt);
    	    ns.play(playlistData.list[hasplayed]);
    		hasplayed += 1;
    	} else {
    		ns.close();
    	}
    }

  2. #2
    Senior Member
    Join Date
    May 2008
    Posts
    332
    Here's code for a hard coded array that you should be able to adapt to your method of creating the array.
    The trick to getting the next video to play is to use the (info.code == "NetStream.Play.Stop") to increment to the next array element.
    Code:
    var nc:NetConnection = new NetConnection();
    nc.connect(null);
    var ns:NetStream = new NetStream(nc);
    video_screen.attachVideo(ns);
    
    // new bit of code to load array of flv strings 
    var myvideoarray:Array=new Array();
    myvideoarray[0] = "video1.flv";
    myvideoarray[1] = "video_12.flv";
    
    var k:Number = 0;
    ns.play(myvideoarray[k]);
    
    ns.setBufferTime(5);
    
    ns.onStatus = function(info) {
    trace(info.code);
    
    if(info.code == "NetStream.Buffer.Full") {
    bufferClip._visible = false;
    }
    if(info.code == "NetStream.Buffer.Empty") {
    bufferClip._visible = true;
    }
    
    if(info.code == "NetStream.Play.Stop") {
    ns.seek(0);
    k++;
    // when all the videos have played, the first video is loaded
    if (k>= myvideoarray.length) {
    k=0;
    }
    ns.play(myvideoarray[k]);
    }
    }
    Best wishes,
    Eye for Video
    www.cidigitalmedia.com

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    10
    Eye for Video,

    Thanks for your reply. Using the thought process behind that, I discovered my problem. This is the code I ended up using:

    Code:
    stop();
    // global objects....
    // playlist object for the playlist from raw xml data
    	var playlistData:Object = new Object();
    	playlistData.list = new Array();
    	
    // Create a videos object to hold a video	
    	var videos:Object = new Object();
    	
    // create variable for keeping count of videos played
    	var hasPlayed:Object = new Object();
    
    // create variable for tracking buffer
    	var hasBuffered:Object = new Object();
    
    	// create variable for total movies to be played
    	var quantity:Object = new Object();
    
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		total = xmlNode.childNodes.length;
    		for (i=0; i<total; i++) {
    			playlistData.list[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    			descriptions.list[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    		}
    		trace ("Loaded "+total+" entries from XML source.");
    	} else {
    		content = "file not loaded!";
    	}
    	trace("Trying to start vids...");
    	hasplayed = 0;
    	hasBuffered = 0;
    	startVids();
    }
    
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    var myXmlFile:String = "xml.xml";
    xmlData.load(myXmlFile);
    
    function startVids() {
    	trace("Made it to start vids. Making a new connection...");
    // Create a NetConnection object:
    	var nc:NetConnection = new NetConnection();
    	trace("Net connection to null...");
    // Create a local streaming connection:
    	nc.connect(null);
    	trace("New netstream connection...");
    // Create a NetStream object and define an onStatus() function:
    	var ns:NetStream = new NetStream(nc);
    	trace("Set the time to buffer");
    // Set the buffer time:
    	ns.setBufferTime(5);
    	trace("Attching netstream to instance of video");
    // Attach the NetStream video feed to the Video object:
    	theVideo.attachVideo(ns);
    
    	theVideo.addEventListener("complete",nsListener);
    	
    	ns.onStatus = function(info) {
    		if(info.code == "NetStream.Buffer.Full") {
    //			Do nothing, future "Buffering" screen
    		}
    		if(info.code == "NetStream.Buffer.Empty") {
    //			if(hasBuffered==0) {
    //			Do nothing, future "Buffering" screen
    //			} else {
    //			Do nothing, future "Buffering" screen
    //			}
    		}
    		if(info.code == "NetStream.Play.Stop") {
    			loadNext();
    		}
    	}
    
    trace("Play the first video in the lineup, which is "+playlistData.list[0]);
    // Being playing the FLV file:
    ns.play(playlistData.list[hasplayed]);
    	trace("Set hasplayed to 1");
    	trace("Get quantity, which is "+playlistData.list.length);
    	quantity = playlistData.list.length;
    
    nsListener.complete = function( evt:Object ):Void {
        loadNext();
    };
    
    
    }
    
    function loadNext() {
    	if(hasplayed < quantity) {
    	    clearInterval(myInt);
    		hasplayed += 1;
    		startVids();
    	    ns.play(playlistData.list[hasplayed]);
    	} else {
    		ns.close();
    	}
    }
    It's quick and dirty, but the whole premise is to have a swf file which was completely independent of the filenames and locations. This swf file could pull in the needed information and just play the list of videos one after another. No pause, no scrub bar, just buffer a few seconds and play.

    I got this code to work this morning, and it does exactly as expected. It creates a 16k swf file that can read the playlist from a formatted XML file which, later, will be a php script to create dynamic playlists. Thought I would share the code here, since I looked for something like this and never found it. Hope that this helps someone out there looking for something like this.

    And thanks again. I love this place.

  4. #4
    Senior Member
    Join Date
    May 2008
    Posts
    332
    Glad you got it working!
    Thanks for sharing your successful code. I appreciate learning how you resolved this.
    I'm adding your code to my library.
    Best wishes,
    EfV

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center