Search code examples
objective-ciosavaudioplayer

(iOS) Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43


NSString *songNameEscaped = [songName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
NSURL *songURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://somerootpath/Music/", songNameEscaped]];

NSLog(@"songURL = %@", songURL);
NSError *avPlayerError = nil;

AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:&avPlayerError];
if (avPlayerError)
{
    NSLog(@"Error: %@", [avPlayerError description]);
}
else
{
    [avPlayer play];
}

If I copy the NSLog output from NSLog(@"songURL = %@", songURL); and paste it in safari, Quicktime plugin plays the files no problem so I know the URLs are valid. I've tried with .m4a and .mp3 files and have tried removing the spaces from songName but not matter what I always get Error:

Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)".

But they are just standard .m4a / .mp3 files created in iTunes.


Solution

  • Apparently AVAudioPlayer doesn't support streaming. Apple recommends AVPlayer for this although it is not as conveinient for finding things like current time and duration .