views:

314

answers:

2

Is it possible in an air application to start a download, pause it and after that resume it?

I want to download very big files (1-3Gb) and I need to be sure if the connection is interrupted, then the next time the user tries to download the file it's start from the last position.

Any ideas and source code samples would be appreciated.

+1  A: 

Yes, you would want to use the URLStream class (URLLoader doesn't support partial downloads) and the HTTP Range header. Note that there are some onerous security restrictions on the Range header, but it should be fine in an AIR application. Here's some untested code that should give you the general idea.

private var _us:URLStream;
private var _buf:ByteArray;
private var _offs:uint;
private var _paused:Boolean;
private var _intervalId:uint;
...
private function init():void {
    _buf = new ByteArray();
    _offs = 0;

    var ur:URLRequest = new URLRequest( ... uri ... );
    _us = new URLStream();

    _paused = false;
    _intervalId = setInterval(500, partialLoad);
}
...
private function partialLoad():void {
    var len:uint = _us.bytesAvailable;
    _us.readBytes(_buf, _offs, len);
    _offs += len;

    if (_paused) {
        _us.close();
        clearInterval(_intervalId);
    }
}
...
private function pause():void {
    _paused = true;
}
...
private function resume():void {
    var ur:URLRequest = new URLRequest(... uri ...);
    ur.requestHeaders = [new URLRequestHeader("Range", "bytes=" + _offs + "-")];
    _us.load(ur);
    _paused = false;
    _intervalId = setInterval(500, partialLoad);
}
Tmdean
A: 

Do you know if there is a way to pause and resume the download of a streaming video in s:VideoPlayer in Flex4? I have a video page in an application, that might start streaming video before all resources for other pages are loaded. When a user starts streaming the video, but wants to switch to other still unloaded content before the video has downloaded completely, the other resources load very slow, so I want to pause the download of the streaming video. When the user comes back to the video later, I need to resume the download to avoid starting the video streaming from the beginning.

David