我需要设置AudioQueueBufferRef的mAudioData。我尝试了copyMemory:
inBuffer.pointee.copyMemory(from: lastItemOfArray, byteCount: byteCount) // byteCount is 512
但是它不起作用。
AudioQueueNewOutput()
队列已正确设置为Int16 pcm格式
这是我的代码:
class CustomObject {
var pcmInt16DataArray = [UnsafeMutableRawPointer]() // this contains pcmInt16 data
}
let callback: AudioQueueOutputCallback = { (
inUserData: UnsafeMutableRawPointer?,
inAQ: AudioQueueRef,
inBuffer: AudioQueueBufferRef) in
guard let aqp: CustomObject = inUserData?.bindMemory(to: CustomObject.self, capacity: 1).pointee else { return }
var numBytes: UInt32 = inBuffer.pointee.mAudioDataBytesCapacity
/// Set inBuffer.pointee.mAudioData to pcmInt16DataArray.popLast()
/// How can I set the mAudioData here??
inBuffer.pointee.mAudioDataByteSize = numBytes
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nil)
}
从苹果文档:https://developer.apple.com/documentation/audiotoolbox/audioqueuebuffer?language=objc
mAudioData:
音频数据拥有音频队列缓冲区。缓冲区地址无法更改。
所以我想解决方案是将新值设置为相同的地址知道怎么做的人吗?
您已经关闭!
尝试一下:
inBuffer.pointee.mAudioData.copyMemory(from: lastItemOfArray, byteCount: numBytes)
或此:
memcpy(inBuffer.pointee.mAudioData, lastItemOfArray, numBytes)
[Audio Queue Services]在纯C语言时足够坚韧,无法使用。现在我们必须做很多桥接工作才能使API与Swift一起使用,这确实是一个痛苦。如果您有选择,请尝试AVAudioEngine。