我使用以下代码播放音频文件。它可以很好地播放 MP3 文件,但是当我尝试播放 AAC 文件时,[[AVAudioPlayer alloc] initWithContentsOfURL:]
返回 nil
并且我收到以下错误:
Error Domain=NSOSStatusErrorDomain Code=1937337955 "The operation couldn’t be completed. (OSStatus error 1937337955.)"
音频文件在 Mac 和 iPhone 上播放良好(当我通过电子邮件发送给自己时)并且在此处: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];
更新:如果我将文件的扩展名从 aac 更改为 m4a,错误代码将更改为 1954115647(其四字母代码为“tip?”)。我用弧扩展得到的错误代码的四个字母代码是“sync”。
最佳答案
使用 initWithData:error:
初始化音频播放器可以解决这个问题。
例如:
NSData* data = [NSData dataWithContentsOfURL:url] ;
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:outError];
令人失望的是苹果基于扩展来初始化他们的音频播放器,而不是查看二进制数据并试图弄清楚他们收到的是哪种类型的数据。
关于ios - AVAudioPlayer initWithContentsOfURL 为 AAC/M4A 文件返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104398/