我一直在尋找一個完整的答案,以實現多個下載的MP3文件downloadManager的web和stackoverflow。我已經閱讀了大量的文章,並意識到最高效的設施是使用ByteArray,URLStream和FileStream來節省t盤。我執行如下:Adobe Air多國下載
<?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>
雖然它似乎捕獲並保存所有的MP3的磁盤,我不斷收到以下錯誤信息。
錯誤#2044:未處理的IOErrorEvent :.文本=錯誤#2032:流錯誤。在multiDownloadSampleApp7/writeComplete()[C:\ Users \ development \ newgageProjects \ multiDownloadSampleApp7 \ src \ multiDownloadSampleApp7.mxml:multiDownloadSampleApp7/downloadNextFile()[C:\ Users \ development \ newgageProjects \ multiDownloadSampleApp7 \ src \ multiDownloadSampleApp7.mxml:40] 59] [卸載SWF] multiDownloadSampleApp7.swf
當我關閉錯誤並查看保存目錄時,我找到了我在數組中請求的所有MP3。
Cadin,謝謝添加一個測試數組是否爲空的函數完美工作。非常感謝!!!!! – user1045857