Play video while download in AIR (FileStream and ByteArray)
August 6th, 2009
Adobe Media Player (powered by AIR) has great progressive download cache functionality for video files - when you start watching a video it begins to download it to the disk, so you can watch it afterwards and don’t have to download it again.

In other words, you can play videos, which are not completely downloaded. In this tutorial, we will show how to implement this in AIR.
Such functionality can be implemented using FileStream class (not using FileReference) as FileStream can write to some place on the disk without asking via dialog for location to save.

Download and try final AIR app
I have created class VideoDownloader.as, which you can easily reuse for this purpose.
continue reading for source and explanation…
The trick is, that you need to continuously save file (FileStream.writeBytes), while downloading it from internet (URLStream.readBytes) and then you just point the VideoDisplay.source to the file.
Before playing you have to download minimal buffer and create a file on the disk, otherwise the video won’t play. So you can’t start download and play in the same time. For this, we wait until 0.5 MB of the video is downloaded and then we start playing. You can set the defaultBufferSize property to adjust it for your videos.
First open URLStream
var req:URLRequest = new URLRequest(urlVideo); urlStream = new URLStream(); urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile); urlStream.addEventListener(Event.COMPLETE,function(event:Event):void{ fileStream.close(); }); urlStream.load(req);
Then read bytes from URLStream and write them to the disk using FileStream.
// chunk of new data var fileDataChunk:ByteArray = new ByteArray(); // read bytes from the internet using URLStream urlStream.readBytes(fileDataChunk,0,urlStream.bytesAvailable); // write bytes to the disk using FileStream fileStream.writeBytes(fileDataChunk,0,fileDataChunk.length);
Tip: Resuming download - generally
It may happen, that you lost internet connection or user closes the AIR app - in this case, download will stop - how to resume stopped downloads?
You need to keep somewhere in local database the last position in FileStream of where you finished with download. Then when opening file, use FileMode.APPEND to continue writing the file. You would also need to implement some script on server, which will start sending the data from current position.
Source: download
Facebook comments:
4 Comments »
RSS feed for comments on this post. / TrackBack URL




Hi,
nice example, but during the installation an error occured…
The application could not be installed because the AIR file is damaged. Try obtaining a new AIR file from the application author.
BTW: pokračuj v dobré práci
Comment by soulWasted — August 6, 2009 @ 4:19 pm
Should be fixed now:
http://flashrealtime.com/tuts/play-while-download-air/VideoDownloaderExample.air
also zipped - just for sure:
http://flashrealtime.com/tuts/play-while-download-air/VideoDownloaderExample.air.zip
Thanks for checking
Comment by tom — August 6, 2009 @ 7:15 pm
Would I be right to say that this will not work on Windows since it’s not possible to write and read to a file at the same time?
Comment by James Hunt — August 19, 2009 @ 8:11 am
@James - probably true. Just tested and it doesn’t work on Windows. On Mac it works. So for Windows, you would probably need to download the video first and then play. Or play it as a HTTP Progressive download and next to it download to a folder.
Comment by tom — August 20, 2009 @ 12:39 pm