我一直在网络和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:。text=错误#2032:流错误。在multiDownloadSampleApp7/downloadNextFile()[C:\Users\development\newgageProjects\multiDownloadSampleApp7\src\multiDownloadSampleApp 7.mxml:40]在multiDownloadSampleApp7/writeComplete([卸载SWF]multiDownloadSampleApp7.SWF
当我关闭错误并在保存目录中查找时,我会在数组中找到我请求的所有MP3。
在调用downloadNextFile()
之前,您似乎并没有检查是否已下载完所有文件。
因此,错误可能只是它到达数组的末尾,然后尝试生成一个带有空字符串的URLRequest。