<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog of the Month &#187; code</title>
	<atom:link href="http://blog.studioofthemonth.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.studioofthemonth.com</link>
	<description>Chronicling the adventures of Studio of the Month.</description>
	<lastBuildDate>Fri, 20 Jan 2012 03:04:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making a randomly looping video player in AS3</title>
		<link>http://blog.studioofthemonth.com/2010/02/16/making-a-randomly-looping-video-player-in-as3/</link>
		<comments>http://blog.studioofthemonth.com/2010/02/16/making-a-randomly-looping-video-player-in-as3/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 07:48:05 +0000</pubDate>
		<dc:creator>Chase</dc:creator>
				<category><![CDATA[clients]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[sotm]]></category>

		<guid isPermaLink="false">http://blog.studioofthemonth.com/?p=173</guid>
		<description><![CDATA[Occasionally a project comes along that sounds SO easy, but turns out to be a big challenge. SotM loves a good challenge so we were excited when a good friend recently came to us with just such a project. The client had a folder filled with 40+ .flv files and they needed a video player [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally a project comes along that sounds SO easy, but turns out to be a big challenge. SotM loves a good challenge so we were excited when a good friend recently came to us with just such a project. The client had a folder filled with 40+ .flv files and they needed a video player that would infinitely play these videos at random. Sounds easy enough but the challenge was making this seemless so the video came across as one never ending clip.</p>
<p>Our first thought was the obvious simple logic. Load and play a video, then when a video completes load and play the next video. The problem with that is the slight gap you get while the request is made for the next file. The solution logic is to have two video players stacked (fp1 and fp2). One player plays a video while the second player is hidden loading the next video. Once the visible player completes, it is hidden and begins loading the next video. Meanwhile the other player is made visible and begins playing.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_player_1512122264"
			class="flashmovie"
			width="558"
			height="311">
	<param name="movie" value="/wp-uploads/2010/02/player.swf" />
	<param name="wmode" value="transparent" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/wp-uploads/2010/02/player.swf"
			name="fm_player_1512122264"
			width="558"
			height="311">
		<param name="wmode" value="transparent" />
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>This is what we will be making. Let&#8217;s have a look at the code. You can download all of our source files for this simple project <a href="http://blog.studioofthemonth.com/wp-uploads/2010/02/random-looper.zip">here</a>.</p>
<pre class="brush: as3;">// set a path to video files
var path:String = &quot;flvs/&quot;;

// generate array of file names
var fileNames:Array = [&quot;one&quot;,&quot;two&quot;,&quot;three&quot;,&quot;four&quot;];</pre>
<p>First create a variable &#8216;path&#8217; to the flv directory. Then create an array of all the flv file names. In our final player we load these up via php and flashVars to allow the client to simply upload videos into a directory. For this example, however, we will add the names manually in an array called fileNames.</p>
<pre class="brush: as3; first-line: 6;">// random whole number generator
function rand(high:Number):Number {
    return int(Math.random() * high);
}</pre>
<p>We want the videos to load at random so next we create a very simple random whole number generator.</p>
<pre class="brush: as3; first-line: 10;">// assign initial video source
fp1.source = path + fileNames[rand(fileNames.length)] + &quot;.flv&quot;;

// create event listeners
fp1.addEventListener(&quot;complete&quot;, nextVideo);
fp2.addEventListener(&quot;complete&quot;, nextVideo);</pre>
<p>Next we setup a few things. We have 2 flvPlayback components on the stage, one named fp1 and another named fp2. We want to set the initial source of fp1 to the first video in the fileNames array. The we give both flvPlaybacks event listeners that will fire our nextVideo function on complete.</p>
<pre class="brush: as3; first-line: 16;">var firstPlayer = true;</pre>
<p>A boolean variable is created to tell the player which flvPlayback is currently visible and playing. If firstPlayer is true, fp1 is playing. If firstPlayer is false, fp2 is playing.</p>
<pre class="brush: as3; first-line: 17;">// load next random video into open player
function nextVideo(e):void {

	// var flv is created and set to a random filename in fileNames array
	var flv = rand(fileNames.length);

	// var p (play) and l (load) are set to equal fp1 and fp2.
	var p = fp1;
	var l = fp2;

	// switch var p and l if the firstPlayer is not playing.
	if (!firstPlayer) {
		p = fp2;
		l = fp1;
	}

	// toggle firstPlayer variable
	firstPlayer = !firstPlayer;

	// set p to visible and play
	p.alpha = 1;
	p.play();

	// set l to invisible,begin loading the next video and set to stop.
	l.alpha = 0;
	l.source = path + fileNames[flv] + &quot;.flv&quot;;
	l.stop();
}</pre>
<p>Now for the function. It&#8217;s actually pretty simple so I think the comments should explain it pretty well.</p>
<pre class="brush: as3; first-line: 45;">nextVideo(null);</pre>
<p>Finally the nextVideo function is called when the .swf first loads. We have to send an argument of null because nextVideo is expecting an event via the listeners.</p>
<p>What we get is a simple Flash based video player that will infinitely play videos at random. Additionally we created a play/pause button, created a check to keep files from repeating back-to-back and added functionality to load file names from a directory via php. I&#8217;m happy to explain these process, just let me know.</p>
<p>Of course one of the neat things about coding is there is always 5-6 ways of solving any problems. What would your solution be?</p>
<p><strong>We should note</strong> that there is a bit of a concern over bandwidth with a player like this. If you have 50 videos and a user leaves the browser window open, the videos will all load. If a couple thousand people do that you could easily eat up 200-300 gigs of transfer. Be careful!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.studioofthemonth.com/2010/02/16/making-a-randomly-looping-video-player-in-as3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

