I observed strange behavior while working with AVAudioPlayer
Following is the code:
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",fileName]] error: &error];
In this, I downloaded the file from server and stored in application's Cache directory.
I am getting following error:
Error in playing =
Domain = NSOSStatusErrorDomain
Code = -43
Description = Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
I also verified that file is present at that location. Everytime I restart my application, I was getting same error for song play. After some time, when I tried to run same code, my player just works fine without any error.
Can anyone tell me how to handle this error?
Also, Can anyone explain me what was the problem?
AVAudioPlayer does not support streaming via HTTP. Try using AVPlayer instead.
I had the same error with this code, even though I could verify that the songCacheURL was valid and that the file was available:
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songCacheURL error:&error];
I was able to fix the issue by loading the file first into an NSData element and then using that to initialize the AVAudioPlayer instead, like so:
NSData *songFile = [[NSData alloc] initWithContentsOfURL:songCacheURL options:NSDataReadingMappedIfSafe error:&error1 ];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:songFile error:&error2];
I hope that helps someone else.
Robin
I had a similar issue. As it turns out, I was initializing the url with just the filename and omitting the bundle resources path. Your url init should look something like:
NSURL *url= [NSBundle URLForResource: @"my_sound" withExtension:@"mp3"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With