17

私の問題は次のとおりです。私はこのコードを取得しました。初期化後に AVAudioPlayer が nil であるため、NSURL が破損していると思います。

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 のテクニカル Q&A 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 に答える