17

我的问题如下。我得到了这段代码,我猜是一个损坏的 NSURL,因为 AVAudioPlayer 在初始化后为零:

NSString *dummyURLString = @"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p";
NSError *error;
NSURL *url = [NSURL URLWithString:dummyURLString]; 
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[player play];

有什么建议吗?这里出了什么问题?

&error 显示了这一点:

Error Domain=NSOSStatusErrorDomain Code=-43 "Operation could not be completed. (OSStatus error -43.)"
4

3 回答 3

37

AVAudioPlayer 仅适用于本地 URL。它必须是文件 URL (file://)

请参阅Apple 的技术问答 QA1634

于 2009-10-22T09:16:49.357 回答
9

我首先尝试了这个,但得到了错误 2003334207:

NSData *soundData = [NSData dataWithContentsOfURL:URL];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:soundData error:&error];

似乎 AVAudioPlayer 真的想要一个文件。所以我先把数据放到一个文件中:

NSURL *url = [NSURL URLWithString:@"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"]; 
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                        NSUserDomainMask, YES) objectAtIndex:0] 
                        stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                fileURLWithPath:filePath] error:NULL];  
NSLog(@"error %@", error);
于 2009-10-22T09:27:06.443 回答
7

您可以使用 AVPlayer,而不是使用 AVAudioPlayer。AVPlayer 也适用于远程 URL

于 2015-11-18T16:21:54.560 回答