本文介绍了Swift3 AudioToolbox: PCM 播放如何AudioQueueAllocateBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 https://github.com/AlesTsurko/LearningCoreAudioWithSwift2.0/tree/master/CH05_Player播放频率,但它是 Swift2.在 Swift 3 中使用音频队列获取麦克风输入解决了很多问题,但它是用来记录的.

I am following https://github.com/AlesTsurko/LearningCoreAudioWithSwift2.0/tree/master/CH05_Playerto 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])

出现错误

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

--在 Spads 回答之后--

--After Spads' answer--

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

我使用的音频描述是

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)

推荐答案

你不应该有一个 AudioQueueBufferRef 数组吗?而不是 AudioQueueBufferRef

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

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

这篇关于Swift3 AudioToolbox: PCM 播放如何AudioQueueAllocateBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 17:49