无法在Swift中创建AudioQueueBufferRef

时间:2017-05-20 20:24:30

标签: ios swift audiotoolbox

ringBuffers = [AudioQueueBufferRef](repeating: AudioQueueBufferRef(), count:inflightBuffersCount)

init() is unavailable: use 'nil' literal

但如果是

ringBuffers = [AudioQueueBufferRef](repeating: nil, count: inflightBuffersCount)

它说

main.swift:152:29: Expression type '[AudioQueueBufferRef]' is ambiguous without more context

如果是

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

打印

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

我认为分配nil不正确

我使用的流描述是

let 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 个答案:

答案 0 :(得分:0)

你错了;)你根本不需要分配任何东西;缓冲区将由您调用的AudioQueueAllocateBuffer函数创建。

例如:

var buffer: AudioQueueBufferRef? = nil
let status = AudioQueueAllocateBuffer(inQueue!, bufferSize, &buffer)