views:

1354

answers:

3

My Problem is the following. I got this code and i guess a corrupt NSURL since the AVAudioPlayer is nil after initializing:

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];

Any suggestions what is going wrong here?

The &error shows this:

Error Domain=NSOSStatusErrorDomain Code=-43 "Operation could not be completed. (OSStatus error -43.)"
+1  A: 

AVAudioPlayer only works with local URL's. It must be a File URL (file://)

See Apple's Technical Q&A QA1634

Niels Castle
A: 

I tried this first but got error 2003334207:

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

Seems that AVAudioPlayer really wants a file. So I put the data into a file first:

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);
mahboudz
Thanks a lot, this solved the first step :)But it seems to me the AVAudioPlayer cannot play back the m4p-file :( ... it does not start to play or respond to any delegate-method :(
tommy
Give the new code a try. I got it to play for a brief moment and then it stopped. Still investigating. Where is the URL from?
mahboudz
A: 

Thanks for the try with saving to a file, but it still did not play at all... I guess its because you save the m4p as a wav file and there is an encoding error...?! But even saving it as m4p does not work at all :(

The link is a pre-listening link to an song provided by a customer ;)

Thanks for investigating!!!!

tommy