5

I use the following code to play an audio file. It plays fine for MP3 files, but when I try to play an AAC file, the [[AVAudioPlayer alloc] initWithContentsOfURL:] returns nil and I get the following error:

Error Domain=NSOSStatusErrorDomain Code=1937337955 "The operation couldn’t be completed. (OSStatus error 1937337955.)"

The audio file plays fine on Mac and iPhone (when I email it to myself) and is here: https://dl.dropboxusercontent.com/u/2667666/song.aac

NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];

// Create audio player with background music
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"aac"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
if (!_backgroundMusicPlayer) {
    NSLog(@"_backgroundMusicPlayer pointer is null!!");
}
[_backgroundMusicPlayer setDelegate:self];  // We need this so we can restart after interruptions
[_backgroundMusicPlayer prepareToPlay];
[_backgroundMusicPlayer play];

Update: If I change the extension of the file from aac to m4a, the error code change to 1954115647 (whose four letter code is "tip?"). The four letter code for the error code that I get with the arc extension is "sync".

RawMean
  • 7,547
  • 3
  • 52
  • 79

3 Answers3

10

Initializing the audio player with initWithData:error: solves this.
For example:

NSData* data = [NSData dataWithContentsOfURL:url] ;
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:outError];

Disappointing that apple initializes their audio player based on extension, instead of looking at the binary data and trying to figure out which type of data they received.

Nir Golan
  • 1,306
  • 10
  • 24
  • This IS the way I initialize the audio player. Still does not work. Can you give more specifics? – MajorHavoc Jan 17 '15 at 19:06
  • @MajorHavoc what is the file type ? how did you encode it etc. ? – Nir Golan Jan 20 '15 at 10:33
  • 1
    The file has an "extension" of aac. The file is a raw capture of an AAC stream that the app captured and saved in the Bundle. The file plays on the Mac as a preview (space bar), in VLC (identifies itself as an AAC file) and in other players as well. It also plays just fine using AVPlayer framework. However, the app needs the call back methods and interface elements of the AVAudioPlayer (I allow jump back and forward 30, seek to next item, etc). Thanks for any help. – MajorHavoc Jan 20 '15 at 16:31
  • do you mind uploading the file @MajorHavoc ? Also, what type of error do you get? does AVAudioPlayer instantiate ? – Nir Golan Jan 27 '15 at 15:26
  • No, I can make plenty of them. Where shall I put it? Can I upload here? Also, the error is as mentioned above: Code=1937337955 – MajorHavoc Jan 27 '15 at 22:07
6

Found the solution!

Turns out that if I simply use AVPlayer instead of AVAudioPlayer and if I use .acc as the extension of the file, then it plays just fine. now I can play both aac and mp3 files.

RawMean
  • 7,547
  • 3
  • 52
  • 79
  • 1
    Sadly, this does not work for me. I need the app delegate methods, the call back functions, and the ability to search and jump easily in the file. Does anyone know why AAC still does not work? Changing to AVPlayer would be a major rewrite for me. Thanks – MajorHavoc Jan 17 '15 at 19:05
  • @MajorHavoc did you find a solution? – Ixx Aug 28 '17 at 14:48
  • @MajorHavoc the solution in the next answer (https://stackoverflow.com/a/21729842/930450) fixed it for me – Ixx Aug 28 '17 at 15:08
1

.aac file can be played,try to check whether the file exists?

   NSFileManager *filemgr = [NSFileManager defaultManager];
   if ([filemgr fileExistsAtPath: backgroundMusicPath ] == YES)
   {
        NSLog(@"find file");
   }
Rahul Sharma
  • 2,623
  • 2
  • 25
  • 38
longhui.hu
  • 11
  • 2
  • Agreed that checking that the file exist is not a bad first step. But based on my code, I already know it exists at this point because I had opened it already before. – MajorHavoc Aug 28 '17 at 18:44