22

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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Tanu
  • 303
  • 1
  • 2
  • 13
  • I got something *very* similar - except that I was able to get a local audio file to play but not a remote file using a URL. Did you ever figure out the meaning and correct response to OSStatus -43? – bradheintz Sep 30 '10 at 15:18
  • The file is not stored at local path of application's cache directory. And I am using that path only. not a remote path. – Tanu Oct 04 '10 at 06:13
  • I am not getting any information on "OSStatus -43". It only states that "The operation couldn’t be completed." – Tanu Oct 04 '10 at 06:13
  • I am getting the same error, and after verification I discovered that safari for iOS have also the problem with this mp3 file – AmineG Aug 22 '11 at 14:49

7 Answers7

27

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

neoneye
  • 50,398
  • 25
  • 166
  • 151
  • 1
    This is definitely the correct answer. The API for AVPlayer is slightly different, but only slightly, and it will play audio via HTTP. HTTP urls will make AVAudioPlayer give an error -43. – Kenny Winker Jul 13 '12 at 00:52
  • but it is difficult to pause song with AVPlayer, it gets stopped on invocation of stop.Though xcode takes time to convert url into data, it is better to use AVAudioPlayer for both pause & stop song, So +1 to @Robin L for the appropriate answer & +1 to you for the perfect knowledge – Niru Mukund Shah Jan 17 '13 at 10:00
  • In iOS 5, it seems that AVPlayer is also required to load a URL from the music library (with the ipod-library:// protocol). AVAudioPlayer handles this just fine in iOS 6 and 7. – arlomedia Dec 16 '13 at 01:39
23

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

Robin L.
  • 246
  • 2
  • 2
  • 2
    In case anyone is considering this as a solution for loading an ipod-library:// URL in iOS 5, the initWithContentsOfURL method will fail with error code 256. – arlomedia Dec 16 '13 at 01:41
2

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"];
frodo2975
  • 10,340
  • 3
  • 34
  • 41
0

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
Igor Vasilev
  • 356
  • 4
  • 6
0

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.

Beleg
  • 362
  • 2
  • 23
0

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

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
shannoga
  • 19,649
  • 20
  • 104
  • 169