[英]Problems on iOS (Objective-C) playing AAC files in AudioQueue
這是我在這里的第一篇文章,希望您對此有所幫助。 我正在使用此代碼來獲取基於AudioQueue的AAC播放,並使用WAV格式對其進行了測試,並且可以正常工作。 當我放入.CAF或.M4A文件,然后觸發play方法時,會發生這種情況:出現此錯誤:
錯誤:> aq> 1608:失敗(-66674); 將停止(66150/0幀)
我一直在Apple開發支持文檔中搜索此錯誤代碼-66674,並說我遇到一個涉及AudioQueuePrime或AudioQueueStart問題的錯誤(目前,AudioQueuePrime不在代碼中,但我也一直在對此進行測試)。 我知道可以使用帶有音頻隊列的AAC,就我而言,我認為這是准確同步聲音的最可靠方法。
- (IBAction)play:(id)sender {
//OSStatus result;
NSArray *audioTracks = [NSArray arrayWithObjects:
@"/Users/mauro_ptt/Documents/XCODE/SimpleAQPlayViewController/Sample01.caf",
nil];
for (id object in audioTracks) {
// Open the audio file from an existing NSString path
NSURL *sndFileURL = [NSURL fileURLWithPath:object];
AudioFileOpenURL((__bridge CFURLRef)sndFileURL, kAudioFileReadPermission, 0, &mAudioFile);
// get audio format
UInt32 dataFormatSize = sizeof(mDataFormat);
AudioFileGetProperty(mAudioFile, kAudioFilePropertyDataFormat, &dataFormatSize, &mDataFormat);
// create playback queue
AudioQueueNewOutput(&mDataFormat, AQOutputCallback, (__bridge void *)(self), CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &mQueue);
// get buffer size, number of packets to read
UInt32 maxPacketSize;
UInt32 propertySize = sizeof(maxPacketSize);
// get the theoretical max packet size without scanning the entire file
AudioFileGetProperty(mAudioFile, kAudioFilePropertyPacketSizeUpperBound, &propertySize, &maxPacketSize);
// get sizes for up to 0.5 seconds of audio
DeriveBufferSize(mDataFormat, maxPacketSize, 0.5, &bufferByteSize, &mNumPacketsToRead);
// allocate packet descriptions array
bool isFormatVBR = (mDataFormat.mBytesPerPacket == 0 || mDataFormat.mFramesPerPacket == 0);
if (isFormatVBR) {
mPacketsDescs = (AudioStreamPacketDescription*) malloc(mNumPacketsToRead * sizeof(AudioStreamPacketDescription));
} else {
mPacketsDescs = NULL;
}
// Get magic cookie (COMPRESSED AAC)
UInt32 cookieSize = sizeof(UInt32);
OSStatus couldNotGetProperty = AudioFileGetPropertyInfo(mAudioFile, kAudioFilePropertyMagicCookieData, &cookieSize, NULL);
if ((couldNotGetProperty == noErr) && cookieSize) {
char *magicCookie = (char *) malloc(cookieSize);
AudioFileGetProperty(mAudioFile, kAudioFilePropertyMagicCookieData, &cookieSize, magicCookie);
AudioQueueSetProperty(mQueue, kAudioQueueProperty_MagicCookie, magicCookie, cookieSize);
free(magicCookie);
}
// Allocate and prime audio queue buffers
mCurrentPacket = 0;
for (int i=0; i < kNumberBuffers; ++i) {
AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]);
AQOutputCallback((__bridge void *)(self), mQueue, mBuffers[i]);
}
mIsRunning = true;
AudioQueueStart(mQueue, NULL);
}
}
提前致謝!
有關AQOutputCallback的問題,在將正確的類型放置在每個函數參數上必須小心。
一個月前,我遇到了類似的問題,但無法解決。 因此最終使用.wav。 大小更大,但幸運的是我只需要播放5個文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.