我一直在搜索 web 和 stackoverflow 以获得实现多个下载 mp3 文件 downloadManager 的完整答案。我已经阅读了许多帖子并意识到最有效的工具是使用 ByteArray、URLStream 和 FileStream 来保存磁盘。我已经实现如下:
<?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/downloadNextFile()[C:\Users\development\newgageProjects\multiDownloadSampleApp7\src\multiDownloadSampleApp7.mxml:40] 在 multiDownloadSampleApp7/writeComplete()[C:\Users\ development\newgageProjects\multiDownloadSampleApp7\src\multiDownloadSampleApp7.mxml: 59] [卸载 SWF] multiDownloadSampleApp7.swf
当我关闭错误并查看保存目录时,我在数组中找到了我请求的所有 MP3。