Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle error code -43 from NSOSStatusErrorDomain when initializing AVAudioPlayer Object?

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?

like image 789
Tanu Avatar asked Sep 01 '10 13:09

Tanu


3 Answers

AVAudioPlayer does not support streaming via HTTP. Try using AVPlayer instead.

like image 100
neoneye Avatar answered Nov 14 '22 12:11

neoneye


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

like image 24
Robin L. Avatar answered Nov 14 '22 13:11

Robin L.


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"];
like image 2
frodo2975 Avatar answered Nov 14 '22 13:11

frodo2975