Adobe Air multi-download

395 Views Asked by At

I have been searching both the web and stackoverflow for a complete answer to implementing multiple downloads of mp3 files downloadManager. I have read a number of post and realize the most efficient facilities are to use ByteArray, URLStream and FileStream for saving t disk. I have implemented as follows:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx"     
creationComplete="downloadFiles()">
<fx:Declarations>

</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.filesystem.*;
import flash.net.URLStream;
public var stream:URLStream;
public var fileData:ByteArray;
public var fileStream:FileStream;
public var file:File; 
public var trackString:String;
public var filearray:Array;
public var urlReq:URLRequest;

private function downloadFiles():void
{
filearray = new Array();
filearray[0]="Crazy";
filearray[1]="Distant Lover";
filearray[2]="Easy Going";
filearray[3]="Give In To Love";
filearray[4]="MuzicAgeErotic Moods";
filearray[5]="MuzicAgeGive In To Love";
filearray[6]="MuzicAgePieces Of Dreams";
filearray[7]="MuzicAgeThere will never be another you";
filearray[8]="Remembered";
filearray[9]="Teach Me Tonight"; 

downloadNextFile();
}
private function downloadNextFile():void{
trackString = filearray.shift();
urlReq = new URLRequest("http://localhost:8080/multiDownloadSampleApp7- debug/MuzicAge/"+trackString+".mp3");
stream = new URLStream();
file = File.documentsDirectory.resolvePath("C:/Users/development/test/"+trackString+".mp3"); 
fileStream = new FileStream(); 
stream.addEventListener(ProgressEvent.PROGRESS, progressBar); 
stream.addEventListener(Event.COMPLETE, writeComplete); 
stream.load(urlReq); 

}

private function writeComplete(evt:Event):void
{     
fileData = new ByteArray();     
stream.readBytes(fileData,0,stream.bytesAvailable); 
fileStream.openAsync(file, FileMode.WRITE)     
fileStream.writeBytes(fileData,0,fileData.length);     
fileStream.close();         
stream.removeEventListener(ProgressEvent.PROGRESS, progressBar);     
stream.removeEventListener(Event.COMPLETE, writeComplete); 

downloadNextFile();
} 

private function progressBar(evt:ProgressEvent):void
{     
// progressBar 

}

]]>
</fx:Script>
</s:WindowedApplication>

Although it appears to capture and save all the MP3’s to disk I am constantly receiving the following error message.
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error. at multiDownloadSampleApp7/downloadNextFile()[C:\Users\development\newgageProjects\multiDownloadSampleApp7\src\multiDownloadSampleApp7.mxml:40] at multiDownloadSampleApp7/writeComplete()[C:\Users\ development \newgageProjects\multiDownloadSampleApp7\src\multiDownloadSampleApp7.mxml:59] [Unload SWF] multiDownloadSampleApp7.swf

When I close the error and look in the save directory I find all the MP3’s I requested in the array.

1

There are 1 best solutions below

1
On

It doesn't look like you're checking to see if you've finished downloading all the files before you call downloadNextFile().

So the error is probably just that it gets to the end of the array and then tries to generate a URLRequest with a null string.