使用ASIHTTPRequest下载音频

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ASIHTTPRequest下载音频相关的知识,希望对你有一定的参考价值。

我正在使用ASIHTTPRequest下载音频,当我检查目标文件夹时,那里没有音频文件。这是示例代码。

在viewdidLoad中创建目标路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
dataPath = [documentsDirectory stringByAppendingPathComponent:@"Rabana"];

这是使用ASIHTTPREQUEST并制作队列的下载音频功能:**

- (IBAction)DownloadOneFile:(UIButton *)sender {
if (!networkQueue) {
    networkQueue = [[ASINetworkQueue alloc] init];
}
[networkQueue reset];
networkQueue.maxConcurrentOperationCount=1;
[networkQueue setDownloadProgressDelegate:_ProgressView];
networkQueue.shouldCancelAllRequestsOnFailure=NO;
[networkQueue setRequestDidFinishSelector:@selector(AudioFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(AudioFetchFailed:)];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://appstabs.com/wahab_audios_files/1.mp3"]];[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@",dataPath]];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",dataPath]];
request.shouldContinueWhenAppEntersBackground=YES;
[request setShowAccurateProgress:YES];
request.allowResumeForFileDownloads=YES;
[networkQueue addOperation:request];
[networkQueue go];
timer = [NSTimer scheduledTimerWithTimeInterval: 0.0
                                              target: self
                                            selector:@selector(onTick)
                                            userInfo: nil repeats:YES];

} - (void)AudioFetchComplete:(ASIHTTPRequest *)request {

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/",dataPath] ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
audioPlayer.delegate=self;
audioPlayer.volume = 1;
[audioPlayer prepareToPlay];

}

但我在“路径”字符串中得到nil并且我的音频播放器没有初始化。

答案

您正在尝试保存在“Rabana”子目录中,但您没有创建子目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
dataPath = [documentsDirectory stringByAppendingPathComponent:@"Rabana"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

代替这个:

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/",dataPath] ofType:@"mp3"];

这个:

NSString *path =    [dataPath stringByAppendingPathComponent:YOUR_FILE_NAME];

以上是关于使用ASIHTTPRequest下载音频的主要内容,如果未能解决你的问题,请参考以下文章

ASIHttpRequest-移动到实时PHP服务器后未创建临时文件

使用 ASIHttpRequest 下载多个图像

使用 ASIHTTPRequest 下载 plist 只能部分工作

ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期

ASIHTTPRequest实现断点下载

ASIHTTPRequest类库简介和使用说明