开发者

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 trie开发者_开发问答d 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"];


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜