ios - 将 C 指针转换为 Swift 3

标签 ios core-audio swift3

我有代码:

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/

相关文章:

ios - 对于频繁重复的常量,我该怎么办?

ruby - 在 Mac 上生成音调/正弦波的简单方法? ( ruby 会很好)

swift - 表达式从 "error?"隐式强制转换为 Any

ios - 无法将类型 '() -> Void' 的值转换为预期的参数类型 '(() -> Void)?'

ios - 应用程序因内存问题而关闭

ios - Appium 检查器看不到 XCUITest Driver 元素的可访问性 ID

iphone - Core Data 不允许我删除没有关系对象的对象

iphone - 如何使用 afconvert 将 .caf 格式转换为 .mp3 格式?

iphone - 在 iOS 中使用 Core Audio 同时播放和录音

ios - Bundle.main.path(forResource :ofType:inDirectory:) returns nil