Tanu
Tanu

Reputation: 313

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?

Upvotes: 22

Views: 19649

Answers (7)

frodo2975
frodo2975

Reputation: 11745

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

Upvotes: 2

Jan Vlcek
Jan Vlcek

Reputation: 1

I had a same problem and I solved it now. The problem was in name of mp3 file. In code was file name "Yes.MP3" but name of file is "Yes.mp3". When I running application in simulator everything is ok, because (I don't know why) simulator isn't case sensitive. When you are running the same code on device this error will occurred in this case.

Upvotes: 0

Beleg
Beleg

Reputation: 362

I had a similar issue, the problem wound up being that my mp4 file target membership check box was unchecked, once I checked it, the file played perfectly.

Upvotes: 0

Igor Vasilev
Igor Vasilev

Reputation: 351

I had a similar issue with AVAudioPlayer while using local-file based URL. As has been established, the trouble was with an incorrect file name, I just didn't notice a leading whitespace in its name. So first of all just check your URLs attentively.

Upvotes: 0

shannoga
shannoga

Reputation: 19869

I had the same error and it appeared to be that the URL was nil. Try to check if your URL object is != nil. Maybe your path is wrong.

Upvotes: 0

Robin L.
Robin L.

Reputation: 246

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

Upvotes: 23

neoneye
neoneye

Reputation: 52201

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

Upvotes: 27

Related Questions