好吧,我已经阅读了很多与我类似的问题,但我仍然无法解决问题。
我打算从 Internet 下载音频文件,然后播放它们。使用 NSURLConnection,我成功地下载了文件。这是我下载文件的代码。
- (IBAction)download:(id)sender
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.dailywav.com/0813/bicycles.wav"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data] ;
} else {NSLog(@"no connection!");
}
然后为了保存和播放文件,我写了以下内容
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didfinishloading Received %d bytes of data",[receivedData length]);
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSLog(@"%@", [documentPaths objectAtIndex:0]);
NSString *documentDirectoryPath = [documentPaths objectAtIndex:0];
NSString *folderPath = [documentDirectoryPath stringByAppendingPathComponent:@"myFolder/doScience.wav"];
[receivedData writeToFile:folderPath atomically:YES];
NSError *error;
AVPlayer * player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:folderPath]];
if (player == nil) {
NSLog(@"AudioPlayer did not load properly: %@", [error description]);
} else {
[player play];
NSLog(@"playing!");
}
}
这是我在运行后从控制台得到的
2013-08-23 13:57:51.636 Download_2[4461:c07] didfinishloading 收到 34512 字节的数据 2013-08-23 13:57:51.637 Download_2[4461:c07] /Users/my_name/Library/Application Support/iPhone Simulator /6.1/Applications/88561BAC-66BF-4774-A626-0CEFEC82D63E/Library/Documentation 2013-08-23 13:57:51.639 下载_2[4461:c07] 播放!
似乎一切正常,但问题是我在运行项目时听不到任何声音!