Swift3 AudioToolbox: PCM playback how to AudioQueueAllocateBuffer?

隐身守侯 提交于 2019-12-25 16:57:48

问题


I am following https://github.com/AlesTsurko/LearningCoreAudioWithSwift2.0/tree/master/CH05_Player to playback a frequency but it is with Swift2. Get microphone input using Audio Queue in Swift 3 has resolved many of the issues but it is for recording.

I am stuck at allocating a buffer to audio queue

var ringBuffers = [AudioQueueBufferRef](repeating:nil, count:3)
AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])

It gives an error

main.swift:152:29: Expression type '[AudioQueueBufferRef]' is ambiguous without more context
main.swift:153:20: Cannot pass immutable value as inout argument: implicit conversion from 'AudioQueueBufferRef' to 'AudioQueueBufferRef?' requires a temporary

--After Spads' answer--

var ringBuffers = [AudioQueueBufferRef?](repeating:nil, count:3)
let status = AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])
print("\(status.description)")

prints

vm_map failed: 0x4 ((os/kern) invalid argument)
4

audio description I have used is

inFormat = AudioStreamBasicDescription(
            mSampleRate:        Double(sampleRate),
            mFormatID:          kAudioFormatLinearPCM,
            mFormatFlags:       kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
            mBytesPerPacket:    UInt32(numChannels * MemoryLayout<UInt16>.size),
            mFramesPerPacket:   1,
            mBytesPerFrame:     UInt32(numChannels * MemoryLayout<UInt16>.size),
            mChannelsPerFrame:  UInt32(numChannels),
            mBitsPerChannel:    UInt32(8 * (MemoryLayout<UInt16>.size)),
            mReserved:          UInt32(0)
        )
AudioQueueNewOutput(&inFormat, AQOutputCallback, &player, nil, nil, 0, &inQueue)

回答1:


Should you not have an array of AudioQueueBufferRef? instead of AudioQueueBufferRef

var ringBuffers = [AudioQueueBufferRef?](repeating:nil, count:3)
AudioQueueAllocateBuffer(inQueue!, bufferSize, &ringBuffers[0])


来源:https://stackoverflow.com/questions/43965064/swift3-audiotoolbox-pcm-playback-how-to-audioqueueallocatebuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!