AVAudioPlayer initWithContentsOfURL returns nil for AAC/M4A files
Asked Answered
E

3

6

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".

Episcopalism answered 20/11, 2013 at 18:49 Comment(2)
See the followingWillenewillet
I did check the link provided. The 4-letter code associated with the error number is "sync"Episcopalism
E
7

Found the solution!

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

Episcopalism answered 21/11, 2013 at 2:22 Comment(3)
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. ThanksNickell
@Nickell did you find a solution?Motherinlaw
@Nickell the solution in the next answer (https://mcmap.net/q/1600573/-avaudioplayer-initwithcontentsofurl-returns-nil-for-aac-m4a-files) fixed it for meMotherinlaw
R
11

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.

Rosewood answered 12/2, 2014 at 13:50 Comment(5)
This IS the way I initialize the audio player. Still does not work. Can you give more specifics?Nickell
@Nickell what is the file type ? how did you encode it etc. ?Rosewood
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.Nickell
do you mind uploading the file @Nickell ? Also, what type of error do you get? does AVAudioPlayer instantiate ?Rosewood
No, I can make plenty of them. Where shall I put it? Can I upload here? Also, the error is as mentioned above: Code=1937337955Nickell
E
7

Found the solution!

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

Episcopalism answered 21/11, 2013 at 2:22 Comment(3)
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. ThanksNickell
@Nickell did you find a solution?Motherinlaw
@Nickell the solution in the next answer (https://mcmap.net/q/1600573/-avaudioplayer-initwithcontentsofurl-returns-nil-for-aac-m4a-files) fixed it for meMotherinlaw
V
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");
   }
Virnelli answered 27/4, 2017 at 3:4 Comment(1)
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.Nickell

© 2022 - 2024 — McMap. All rights reserved.