<?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>mandarin.no &#187; video</title>
	<atom:link href="http://mandarin.no/tag/video/feed/" rel="self" type="application/rss+xml" />
	<link>http://mandarin.no</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 09:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making a YouTube-esque video player in Flex</title>
		<link>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/</link>
		<comments>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 11:44:15 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[buffering]]></category>
		<category><![CDATA[custom preloader]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[FLV]]></category>
		<category><![CDATA[MetadataReceived]]></category>
		<category><![CDATA[NetConnection]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[VideoDisplay]]></category>
		<guid isPermaLink="false">http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/</guid>
		<description><![CDATA[Earlier this week I finished a project for one of our clients. The client produces quite lot of video content, and wanted a video player that could be embedded in other websites. This was a rather straight forward project where I got to use a lot of things I already knew. But still I learned [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this week I finished a project for one of our clients. The client produces quite lot of video content, and wanted a video player that could be embedded in other websites. This was a rather straight forward project where I got to use a lot of things I already knew. But still I learned a few new things.</p>
<p>One of the requirements was the file size of the finished SWF. I&#8217;d like to keep it to a minimum with no preloading before the buffering starts. It has to look good, so some graphics will have to be included, but the video is what people wants to see. One of the tricks to reduce filesize and loading time is avoiding using container classes like the canvas, hbox, vbox and the likes. The container classes are handy and useful for laying out your components. But they contain a lot of functionality I didn&#8217;t need, and whenever you add a component to a container class, it will redraw itself. So the initialization process will be slower. (Note to self; put all components inn the container before you add it to stage. That will reduce number of redraws)</p>
<p>As a general rule; if all you need to do is place the component at a particular coordinate; try avoiding using container classes and rather use the x, y properties of the component itself. It is very tempting to let the container do the positioning for you, but with a few extra lines of code, you can most likely avoid using them.</p>
<p>So I discarded the thought of using any containers at all. So how do I approach the placement of the components? Well, first of all I need to find the dimensions of the FLV, and then place the control bar accordingly.</p>
<p>When I first started making Flex apps, I read a lot of articles about developing with Flex. One of them was about the application startup order. I realized that was a subject worth paying attention to, but never did. And all of my Flex apps so far have worked fine. But this time I started to understand the importance of knowing how your Flex app builds itself up.</p>
<p>The video player reads a few FlashVars at startup, one of them being the URL to the FLV. Now, the dimensions of the FLV can vary from file to file, so sending the width and height will only cause more work to be done server side. Flex (and Flash) have a very easy to use event for retrieving the meta data of a FLV. So I decided to keep the number of FlashVars down to a minimum, and rather rely on the MetadataReceived event.</p>
<p>The MetadataReceived event doesn&#8217;t fire until the first part of the FLV has been read. So, since the layout of the video player is waiting for the dimensions of the FLV, I have would have to put this function at the earliest possible stage of the creation process. Sounds easy, but there was one thing that I didn&#8217;t think would matter but did; buffer time.</p>
<p>I was thinking about implementing some kind of dynamic buffering routine to take in account of the various bandwith&#8217;s people have. But the video&#8217;s are rarely longer than 30 seconds and on an average of about 6-7MB. So I figured it would be sufficient to just set the buffering to about 25-30% of the total playtime. And so I did.</p>
<p>Now, what happends when you set the bufferTime property of the videoDisplay component to e.g. 5 seconds, is that videoDisplay will wait until the buffer is full before triggering the MetadataReceived event. I don&#8217;t see any logical reason for this, but that&#8217;s the way it is. The solution to this is simply setting the bufferTime to 0 at initialization. The MetadataReceived will then trigger immediately, giving me all of the details on the FLV I needed. In the event handler for the Metadata-event I simply adjust the bufferTime up to the desired level. It is important to set the autoplay attribute of the VideoDisplay to false, or else the video will try to play immediately since the buffertime is 0.</p>
<p>At this time I have all of the information I need for placing the control bar. In the event handler for MetadataReceived I simply set the control bar&#8217;s y parameter to the height of the video. And voila! The layout is finished, and it all happend withing a fraction of a second.</p>
<p>To give the viewer an indication of the buffertime, I simply used a ProgressBar component. And on top of that I put an HSlider for scrubbing. In retrospect I now see that the ProgressBar is overkill for this purpose. It features a lot of functionality I don&#8217;t use, and I could have managed to achieve the same result using a Sprite which I scale in width (just like with the good old preloaders we made when AS1 and 2 was the big thing).</p>
<p>I mentioned earlier that I didn&#8217;t want any preloading of the application. But Flex throws in a preloader no matter what. In many cases this is a blessing, because you don&#8217;t have to make it yourself. But in this case, it&#8217;s a fair bit annoying. I guess it has to be there because of the creation process, which can take a few seconds -even on tiny applications. So my only option was to make it not look like the standard Flex preloader. I found a few articles on customizing the Flex preloader which helped me. Luckily, this doesn&#8217;t take more than a second in this application, so I simply used the client&#8217;s logo as a preloader. No indication of any progress at all.</p>
<p>These are the few things I learned while working on this project, which will most likely help me attack my next project in a better and more efficient way. Now, there&#8217;s one more thing I&#8217;d like to make a note of. And that&#8217;s something I didn&#8217;t learn.</p>
<p>It seems to me the VideoDisplay component doesn&#8217;t have any events for when the file is not found. I use the debugger version of the Flash Player, and that throws me a large error message saying the file was not found. It would&#8217;ve been better (in terms of usability) for me to trace this error using events instead, and then outputting a message onscreen saying &#8220;file not found&#8221;, or even showing a video where someone says &#8220;Sorry, file not found. But you might want to look at these videos&#8230;&#8221;. I could probably have used NetConnection. If there are anyone out there with an idea, or even better; an answer to this, please leave a comment. I&#8217;d love to hear about it.</p>
<p>Resources<br />
<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=layoutperformance_03.html">Flex 3 &#8211; startup order</a><br />
<a href="http://www.slideshare.net/dcoletta/optimizing-flex-applications">David Colettas slideshow on Flex optimization</a>. It&#8217;s only a slideshow, but there are a few good tips to pick up.<br />
<a href="http://blog.poweredbyhamsters.com/2007/10/02/optimizing-flex-apps.aspx">Powered By Hamsters</a> has a few good tips on optimizing a Flex app.</p>
<p><a href="http://www.alagad.com/go/blog-entry/skinning-with-flex-3-css-explorer">Alagad &#8211; Skinning with Flex 3 CSS Explorer</a><br />
<a href="http://www.adobe.com/devnet/flex/articles/skins_styles.html">Adobe Devnet &#8211; Using CS3 to design styles and skins for Flex 3</a><br />
<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=skinning_3.html">Flex 3 Help &#8211; Applying skins</a>. Some code examples of how to add skins to your components</p>
<p><a href="http://www.onflex.org/ted/2006/07/flex-2-preloaders-swf-png-gif-examples.php">Ted Parick &#8211; Flex custom preloaders</a><br />
<a href="http://flexiblemyself.blogspot.com/2007/10/custom-preloader.html">FLEXibleMySelf &#8211; Custom Preloader (Flex 3)</a></p>
<p>Always helpful:<br />
<a href="http://livedocs.adobe.com/flex/3/langref/index.html">Flex 3 Language Reference</a><br />
<a href="http://examples.adobe.com/flex3/consulting/styleexplorer/Flex3StyleExplorer.html">Flex 3 Style Explorer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
