2010-05-24 66 views
1

我有一款iPhone应用程序可以从网络上下载多个WAV文件,将结果声音数据存储在手机上供在应用程序中播放。有时候这工作得很好,有时我得到的两个问题的声音之一:在iPhone上下载/播放静态音频文件

1)有一个下载的声音文件的各个部分是播放作为staticky,损坏的噪音

2)有时全下载似乎不会发生,这意味着二进制数据不可识别为合法的声音文件,并扼杀我的播放库(FMOD),导致崩溃。

我正在使用NSURLConnection通过附加从连接接收的数据来构建数据,如Apple的文档中所述。我的问题是:我如何编写我的下载代码,以确保所有文件都得到下载,并且它们在没有损坏/噪音的情况下被下载?

事实:

  • 下载是从Amazon S3发生。
  • 它们的大小最多为半个兆字节。
  • 它们在服务器上没有损坏 - 即使它们在手机上搞砸了,它们在浏览器中播放也很好。

下面是我用来下载每个应用程序启动时所有未下载的文件的代码。预先感谢任何帮助!

- (void)downloadOutstandingFileSounds{ 
    NSArray *theFiles = [File listOfDownloadableOpponentfiles]; 

    if ([theFiles count] > 0) { 
     NSLog(@"Updating %d new files...", [theFiles count]); 
     for (File *file in theFiles) { 
      self.theFile = file;   
      NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:file.publicUrl] 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60.0]; 
      NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
      if (theConnection) { 
       currentFileDataContainer = [[NSMutableData data] retain]; 
      }else { 
       NSLog(@"failed attempt to download resource at: %@", file.publicUrl); 
      } 

     } 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"OutstandingFileSoundsDownloaded" object:self]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    [currentFileDataContainer setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    [connection release]; 
    [currentFileDataContainer release]; 

    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data{ 
    [currentFileDataContainer appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSLog(@"Success! Received %d bytes of file data", [currentFileDataContainer length]); 
    self.theFile.fileSoundData = currentFileDataContainer; 
    [self.theFile save]; 
    NSLog(@"Downloadable files count: %d", [[File listOfDownloadableOpponentfiles] count]); 
    self.theFile = nil; 
    [File clearCache]; 
} 

回答

1

我看不出什么太不好的做法,但看看ASIHTTPRequest库http://allseeing-i.com/ASIHTTPRequest/这可以用来排队的一些下载和管理的错误。

+0

很酷。我曾经使用过ASI,但从来没有需要它来解决这个问题。我喜欢ASI的操作队列方面,但我认为它的gzip支持可能是真正的东西,可能可以解决我的问题在这里 - 感谢指出我回到它。 – trevrosen 2010-05-24 15:39:32

+0

顺便说一句,你似乎已经问了一些问题,但没有接受任何答案。你应该回头看看你所提出的问题,并接受正确的答案(假设有一个答案):这就是本网站的内容! – Andiih 2010-05-24 17:30:29

+0

这就是“回答你的问题”按钮的意思吗?我正在寻找如何做到这一点(接受)。我会更难看。 :-) ASI竟然为我解决这个问题。我没有看过代码,但我怀疑它只是在传输过程中更好地解决了错误。 ASINetworkQueue类非常好。我一定会更经常地使用这个库。它甚至对S3 API进行了初步支持! – trevrosen 2010-05-26 14:15:08