iOS(Objective-C)在AudioQueue中播放AAC文件的问题

时间:2014-02-23 00:53:09

标签: ios objective-c audioqueue audioqueueservices

这是我在这里的第一篇文章,希望你能帮助我。我正在研究这个代码以获得基于AudioQueue的AAC播放,我用WAV格式进行了测试,并且它可以工作。当我放入.CAF或.M4A文件然后触发播放方法时会发生这种情况,这个错误出现了:

  

错误:> aq> 1608:失败(-66674);将停止(66150/0帧)

我一直在Apple的Dev支持文档中搜索此错误代码-66674,并说我有一个错误涉及AudioQueuePrime或AudioQueueStart的问题(现在AudioQueuePrime不在代码上,但我一直在测试这个也)。我知道可以使用带有AudioQueues的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);

    }
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

有关AQOutputCallback的问题,在每个函数参数上放置正确的类型时一定要小心。

答案 1 :(得分:0)

一个月前我遇到过类似的问题,我无法理解。所以最终使用.wav。尺寸更大但幸运的是我只需要播放5个文件。