使用空缓冲区调用的AudioQueueInputCallback

时间:2019-06-14 14:34:43

标签: ios swift iphone audioqueueservices

我尝试用iPhone麦克风录制PCM音频,但是当我启动音频队列时,它会在调用我的回调函数后自动停止,直到有缓冲区为止。以我为例3次。这是我的代码:

class AudioModel {
    private var inQueue: AudioQueueRef!

    init() {
        // Describe PCM format. 8kHz, 16bits, mono
        var inFormat = AudioStreamBasicDescription(mSampleRate: 8000.0, // 8kb
            mFormatID: kAudioFormatLinearPCM,
            mFormatFlags: kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
            mBytesPerPacket: 2,
            mFramesPerPacket: 1,
            mBytesPerFrame: 2,
            mChannelsPerFrame: 1,
            mBitsPerChannel: 16,
            mReserved: 0)

        AudioQueueNewInput(&inFormat, audioQueueInputCallback, nil, nil, nil, 0, &inQueue)

        for _ in 0...2 {
            var bufferRef: AudioQueueBufferRef!
            AudioQueueAllocateBuffer(inQueue, 320, &bufferRef)
            AudioQueueEnqueueBuffer(inQueue, bufferRef, 0, nil)
        }
    }

    func startRecord() {
        AudioQueueStart(inQueue, nil)
    }

    func pauseRecord() {
        AudioQueuePause(inQueue)
    }

    private let audioQueueInputCallback: AudioQueueInputCallback = { (inUserData, inAQ, inBuffer, inStartTime, inNumberPacketDescriptions, inPacketDescs) in
        print("AudioQueueInputCallback called")
        print("mAudioDataByteSize: \(inBuffer.pointee.mAudioDataByteSize)")
        var err = AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nil)
        if err != noErr {
            print("AudioQueueEnqueueBuffer err: \(err)")
            return
        }
    }
}

当我打电话给startRecord时,我已经把这个拨出了,什么都没了

AudioQueueInputCallback called
mAudioDataByteSize: 0
AudioQueueInputCallback called
mAudioDataByteSize: 0
AudioQueueInputCallback called
mAudioDataByteSize: 0

在使用Audio Queue Services API进行2周的开发后,仍然有些迷惑我。

0 个答案:

没有答案