我有代码:
let data = Data(bytes: UnsafePointer<UInt8>(audioBuffer.mData), count: Int(bufferSize))
和
let u16 = UnsafePointer<Int32>(audioBuffer.mData).pointee
两者都可以在 Swift 2.3 中工作,但不能在 Swift 3 中工作。如何转换它们以使它们的行为等效? (为什么?)
最佳答案
要从 Swift 3 中的音频单元回调缓冲区读取 16 位音频样本,我使用:
let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData)
if var bptr = bufferPointer {
for i in 0..<(Int(frameCount)) {
let oneSampleI16 = bptr.assumingMemoryBound(to: Int16.self).pointee
// do something with the audio sample
bptr += 1
}
}
Audio Session 和音频单元代码的其余部分如下:https://gist.github.com/hotpaw2/630a466cc830e3d129b9
关于ios - 将 C 指针转换为 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39557496/