<?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>AskApache &#187; Search Results  &#187;  preload</title>
	<atom:link href="http://www.askapache.com/search/preload/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.askapache.com</link>
	<description>Advanced Web Development</description>
	<lastBuildDate>Thu, 26 Apr 2012 11:29:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Preloading .flv and .mp3 files with Flash</title>
		<link>http://www.askapache.com/optimize/preloading-caching-flv-mp3.html</link>
		<comments>http://www.askapache.com/optimize/preloading-caching-flv-mp3.html#comments</comments>
		<pubDate>Tue, 05 Aug 2008 16:30:16 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.askapache.com/flash/preloading-caching-flv-mp3.html</guid>
		<description><![CDATA[<p><a class="IFL" rel="lb" href='http://www.askapache.com/flash/preloading-caching-flv-mp3.html' title='Preload/Cache .mp3 .flv files with Flash Actionscript'><img src='http://uploads.askapache.com/2008/02/myflash1.thumbnail.jpg' alt='Preload/Cache .mp3 .flv files with Flash Actionscript' /></a>If you want to pre-load <code>.flv</code> / <code>.mp3</code> files into a visitors browser cache using flash, here's the actionscript I use to do it, and some ideas behind a good javascript implementation using swfobject or ufo.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/optimize/preloading-caching-flv-mp3.html"></a><a href="http://www.askapache.com/optimize/preloading-caching-flv-mp3.html"><cite>AskApache.com</cite></a></p><p><a class="IFL" rel="lb" href='http://www.askapache.com/flash/preloading-caching-flv-mp3.html' title='Preload/Cache .mp3 .flv files with Flash Actionscript'><img src='http://uploads.askapache.com/2008/02/myflash1.thumbnail.jpg' alt='Preload/Cache .mp3 .flv files with Flash Actionscript' title="myflash1.thumbnail optimize" /></a>The company I work for does <em>tons</em> of online videos for our clients, and being the lead web designer I am responsible for making the videos work flawlessly and instantly.  Fortunately for my clients, I am a little obsessive about finding the best of the best of the best way to do things.  I have a LONG way to go, especially when it comes to flash, but here are a few choice tips and tricks that currently work for my clients.<br class="C" /></p>


<p class="anote">You can read an alternate/older explanation of this <a href="http://www.askapache.com/htaccess/preload-and-cache-flash-flv-files.html">method here</a>.</p>


<h2>Preloading files into Browser Cache</h2>
<p>One thing I like to do that no-one can appreciate except me and maybe you is to preload the sites various .flv and .mp3 files into the site visitors cache.  Let me give you an example.  Site.com has 12 videos spread out on multiple pages, all are .flv files controlled by a single .swf flash video player.  Most visitors arrive at the home page which has 1 of the 12 videos on it.  So the trick is to preload the other 11 .flv video files from the home page.  Then when the visitor goes to a different page with a different video, it loads instantly.</p>

<h2>Preloading .flv files with Actionscript</h2>
<p>The actionscript is easy, it opens a connection to the .flv file, sets the buffertime to 0, plays the video and immediately pauses, then seeks the position back to 0.</p>
<p>Its relatively easy to get this to work, heres all the actionscript you will need to preload any .flv file.  Just create a new flash document, and on frame 1 enter in this actionscript.  You can also search your flash help for "preloading" for more help.</p>
<pre>var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
//attach videodisplay
_level0.attachVideo(ns);
ns.setBufferTime(0);
ns.play(&#039;http://static.askapache.com/f/flv/create-boot-ini.flv&#039;);
ns.pause();
ns.seek(0);</pre>


<h2>Preloading .mp3 files with Actionscript</h2>
<p>This is even easier than .flv!  This is all you need and the mp3 file will be requested from the browser and added to the browsers cache.</p>
<pre>var songTrack:Sound = new Sound();
songTrack.loadSound("http://static.askapache.com/f/mp3/ga-urchin-speed.mp3", true);</pre>


<h2>Little Tricks and Tips</h2>

<h3>Passing filenames with flashvars</h3>
<p>So now you have the actionscript and files, but what if you want to pass the file using flashvars?  So <code>/preloader.swf?file=http://static.askapache.com/f/flv/create-boot-ini.flv</code> will work?</p>
<pre>if (!_root.file) {
  file = &#039;http://static.askapache.com/f/flv/create-boot-ini.flv&#039;;
} else {
  file = _root.file;
}
&nbsp;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
//attach videodisplay
_level0.attachVideo(ns);
ns.setBufferTime(0);
ns.play(file);
ns.pause();
ns.seek(0);</pre>


<h3>Using javascript to time the downloads</h3>
<p>Now, you may think you are a hotshot if you preload 12 .flv files from the homepage using swfobject or ufo, but the problem with doing that all at once is that flash has some real serious issues with running multiple instances like that.  Meaning the movie that IS on your homepage might very well FREEZE or stutter mid-play if you are trying to preload all these files in the background.  I won't get into advanced stuff here but to start with you should at least utilize something like a setTimeout function.  I rely heavily on javascript loops to do this.</p>
<pre>setTimeout(function(){swfobject or ufo code;},10000); // 10 second wait</pre>

<h3>Waiting till fully buffered</h3>
<p>Depending on your .flv video player .swf, you may or may not have the capability to receive information on whether or not a file has been loaded or is fully buffered.  So an additional step that I personally take is I don't start the preloading loop code until the 1st movie on the home-page has fully buffered.  Sometimes I wait until its done playing.</p>


<h2>Apache .htaccess</h2>
<p>Of course, I can't forget this bit of .htaccess</p>
<pre>AddType video/x-flv .flv
AddType application/x-shockwave-flash .swf
&nbsp;
&lt;filesMatch "\.(flv|swf|mp3)$"&gt;
Header unset Pragma
FileETag None
Header unset ETag
Header unset P3P
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
&lt;/filesMatch&gt;</pre>





<h2>Feeling Lucky?</h2>
<p>If by lucky you mean you dig learning how to make your sites FAST, heres some money.</p>
<p><strong>Easy Apache Speed Tips Articles</strong></p>
<ul>
<li><a href="http://www.askapache.com/htaccess/apache-speed-compression.html" title="Turn On gzip Compression to Speed Up Apache">Turn On Compression</a></li>

<li><a href="http://www.askapache.com/htaccess/apache-speed-expires.html" title="Add Far Future Expires Headers to Speed Up Your Site">Add Future Expires Header</a></li>
<li><a href="http://www.askapache.com/htaccess/apache-speed-cache-control.html" title="Cache-Control Headers using Apache and .htaccess">Add Cache-Control Headers</a></li>
<li><a href="http://www.askapache.com/htaccess/apache-speed-etags.html" title="Remove ETag Headers to Speed up Apache">Turn Off ETags</a></li>
<li><a href="http://www.askapache.com/htaccess/apache-speed-last-modified.html" title="Remove the Last-Modified Header from Files to Speed Apache">Remove Last-Modified Header</a></li>
<li><a href="http://www.askapache.com/htaccess/apache-speed-subdomains.html" title="Multiple Subdomains for a Quicker and Faster Web Site">Use Multiple SubDomains</a></li>
</ul><p><a href="http://www.askapache.com/optimize/preloading-caching-flv-mp3.html"></a><a href="http://www.askapache.com/optimize/preloading-caching-flv-mp3.html">Preloading .flv and .mp3 files with Flash</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/optimize/preloading-caching-flv-mp3.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
<enclosure url="http://static.askapache.com/f/flv/create-boot-ini.flv" length="1027853" type="video/x-flv" />
<enclosure url="http://static.askapache.com/f/mp3/ga-urchin-speed.mp3" length="49152" type="audio/mpeg" />
<enclosure url="http://static.askapache.com/f/flv/create-boot-ini.flv" length="1027853" type="video/x-flv" />
<enclosure url="http://static.askapache.com/f/mp3/ga-urchin-speed.mp3" length="49152" type="audio/mpeg" />
		</item>
		<item>
		<title>Preload flash .flv files into browser cache</title>
		<link>http://www.askapache.com/optimize/preload-cache-flash-flv-files.html</link>
		<comments>http://www.askapache.com/optimize/preload-cache-flash-flv-files.html#comments</comments>
		<pubDate>Mon, 04 Feb 2008 15:15:06 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.askapache.com.com/htaccess/preload-and-cache-flash-flv-files.html</guid>
		<description><![CDATA[<p>How I was able to <a href="http://www.askapache.com/htaccess/preload-and-cache-flash-flv-files.html">preload many flash flv and swf files</a> on one of my clients sites that has a lot of online video and relatively small traffic.  Their site visitors would usually watch 3-10 videos per visit and so to make the videos load almost instantly on every page I came up with a way to preload the top 10 .flv files and the swf flv player files as soon as the visitor successfully started watching the 1st video.  Of course I also setup .htaccess caching on the server so that once they downloaded the files into their cache they would never request them from the server again.  I was having fun with this so its pretty funky and uses some really cool combinations of javascript, swf preloader from xml, css classes to help automate it all..</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/optimize/preload-cache-flash-flv-files.html"></a><a href="http://www.askapache.com/optimize/preload-cache-flash-flv-files.html"><cite>AskApache.com</cite></a></p><p><img class="IFR" src='http://uploads.askapache.com/2007/12/flashplayer.gif' alt='Adobe Flash Positioning' title="flashplayer optimize" /><strong>Found a great way to preload and cache flash</strong> .flv video files.  Recently (<em>2006</em>) I was hired as the lead web-designer for an insurance company website with over 30 online videos to be placed throughout the site.  My area of expertise when it comes to Web Development is web-standards, and pretty much anything else open-source, <em>NOT</em> flash.  I started programming Flash back when it first came out, version 1.x, (and I think it was called something else..) because I was learning it for my job at the ISP..  The main reason I don't spend my time learning it (although actionscript is very very rad) is because I'm not a designer.  I don't have the patience for colors and images and effects, that's boring to me, especially because I'm not gifted in the art department.<br class="C" /></p>
<p>My employer is a video production firm, so I use flash (for video and small Advertisements and logos) on pretty much every site I work on, now... this article shows what I learned while building the insurance site and the solutions we came up with for it.</p>
<p class="enote"><strong>Note:</strong> This site had only 50-100 site visitors/day and each visitor typically viewed 3-10 of the videos, so this type of aggressive caching and bandwidth-raising techniques should be looked at on a site-by-site basis.</p>
<hr />

<p>After using the site for a month I noticed that 8 of the 30 videos on the site were being using by almost every site visitor.  To speed up the site I created a very small swf (<em>flash 7</em>) file that uses very minimal actionscript to preload flvs specified in a preloadlist.xml file</p>

<p>Any page that I want the <strong>flv files to be preloaded</strong> I have to have an element with an id of "preloading" or the javascript does not load the swf file.  So I set it up so that the preloading only happens when visitors goto the homepage (where they typically land), and also only when visitors <strong>haven't already preloaded the flvs</strong>.  To check for this I set a cookie when the flvs are preloaded.</p>


<p class="anote">Note there is a newer/briefer <a href="http://www.askapache.com/flash/preloading-caching-flv-mp3.html">update to this technique</a>.</p>
<hr class="C" />


<h2>CSS for preloading</h2>
<p>If you give an element a <code>display:none</code> css attribute, it doesn't work for everyone.  Once the swf loads you can of course change it to none as the preloader initiates the requests onload.</p>
<pre>#preloading {position:absolute; bottom:0;left:0; display:block; width:1px; height:1px; overflow:hidden; z-index:1;}</pre>


<h2>XHTML for preloading</h2>
<p>This just starts the preloadflv function AFTER the HTML file has been loaded... (nowadays I ONLY do unobtrusive javascript and keep everything in an external .js)  Also note that some browsers/flash versions need the preloading span element to be at the top of the html if your html page scrolls down past the window's viewable area.</p>
<pre>&lt;body onload="preloadflv();"&gt;
&lt;span id="preloading"&gt;&lt;/span&gt;</pre>
<hr class="C" />


<h2>Actionscript for preloadflv.fla</h2>
<p>This is literally the whole file, I spent weeks researching to come up with this optimized and customized swf prefloader.  One useful feature is by using the external .xml file you can preload any files dynamically.  Just make sure you setup the caching correctly, don't cache the xml file for very long!  To really appreciate this preloader you should definately use an HTTP capture program like <a href="http://wireshark.askapache.com/">WireShark</a> and also watch your Apache access logs on your server.</p>
<pre>var netPath:NetConnection = new NetConnection();
netPath.connect(null);
var ncPreLD:NetStream = new NetStream(netPath);
var i:Number = 1;
var preloaderXML:XML = new XML();
preloaderXML.ignoreWhite = true;
preloaderXML.onLoad = function(success:Boolean):Void  {
  if (success) {
    var flvFiles:Array = preloaderXML.firstChild.childNodes;
    var flvDir:String = preloaderXML.firstChild.attributes.basedir;
    ncPreLD.play(flvDir+preloaderXML.firstChild.childNodes[0].attributes.path+".flv");
    ncPreLD.onStatus = function(infoObject:Object):Void  {
      if (infoObject.code == "NetStream.Play.Start") {
        ncPreLD.pause();
        if (i&lt;flvFiles.length) {
          ncPreLD.play(flvDir+preloaderXML.firstChild.childNodes[i].attributes.path+".flv");
          i++;
        }
      }
    };
  }
};
preloaderXML.load("preloadlist.xml");</pre>
<hr class="C" />


<h2>XML file preloadlist.xml</h2>
<p>Save this file in the directory of your preloader (unless you modify the .fla) and basically just list all the files to preload from the basedir url.</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;preloader basedir="http://static.askapache.com/f/flv/"&gt;
  &lt;flv path="404-error-page.flv
  &lt;flv path="404-google-plugin.flv
  &lt;flv path="anonymous.flv
  &lt;flv path="create-boot-ini.flv
  &lt;flv path="install-recovery-console.flv
  &lt;flv path="mysql-backup-script.flv
  &lt;flv path="php-ajax-shell.flv
  &lt;flv path="setup-recovery-console.flv
&lt;/preloader&gt;</pre>



<h3>NetStream</h3>
<h4>The NetStream.onStatus</h4>
<dl>
<dt>NetStream.Buffer.Empty</dt>
<dd>statusData is not being received quickly enough to fill the buffer. Data flow will be interrupted until the buffer refills, at which time a NetStream.Buffer.Full message will be sent and the stream will begin playing again.</dd>
<dt>NetStream.Buffer.Full</dt>
<dd>status The buffer is full and the stream will begin playing.</dd>
<dt>NetStream.Buffer.Flush</dt>
<dd>status Data has finished streaming, and the remaining buffer will be emptied.</dd>
<dt>NetStream.Play.Start </dt>
<dd>status Playback has started.</dd>
<dt>NetStream.Play.Stop</dt>
<dd>status Playback has stopped.</dd>
<dt>NetStream.Play.StreamNotFound</dt>
<dd>error The FLV passed to the play() method can't be found.</dd>
<dt>NetStream.Seek.InvalidTime</dt>
<dd>error For video downloaded with progressive download, the user has tried to seek or play past the end of the video data that has downloaded thus far, or past the end of the video once the entire file has downloaded. The message.details property contains a time code that indicates the last valid position to which the user can seek.</dd>
<dt>NetStream.Seek.Notify</dt>
<dd>status The seek operation is complete.</dd>
</dl>



<h2>.htaccess flash and flv</h2>
<p>Make sure apache sends the correct content-type headers with the swf and flv files</p>
<pre>AddType video/x-flv .flv
AddType application/x-shockwave-flash .swf</pre>


<h2>Implement HTTP Caching</h2>
<p>Please see: <a href="http://www.askapache.com/htaccess/mod_rewrite-fix-for-caching-updated-files.html">.htaccess Caching Tutorial</a></p>
<pre># 1 YEAR
&lt;filesMatch "\.(ico|pdf|flv)$"&gt;
Header set Expires "Mon, 27 Mar 2038 20:59:12 GMT"
Header set Cache-Control "max-age=29030400"
&lt;/filesMatch&gt;
# 1 WEEK
&lt;filesMatch "\.(jpg|jpeg|png|gif|swf)$"&gt;
Header set Cache-Control "max-age=604800"
&lt;/filesMatch&gt;
# 3 HOUR
&lt;filesMatch "\.(css|xml|txt|js|html)$"&gt;
Header set Cache-Control "max-age=10800,must-revalidate"
&lt;/filesMatch&gt;</pre><p><a href="http://www.askapache.com/optimize/preload-cache-flash-flv-files.html"></a><a href="http://www.askapache.com/optimize/preload-cache-flash-flv-files.html">Preload flash .flv files into browser cache</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/optimize/preload-cache-flash-flv-files.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

