【问题标题】:How to write buffer data to the middle of a file instead of appending to the end of the file?如何将缓冲区数据写入文件中间而不是追加到文件末尾?
【发布时间】:2020-11-10 00:48:00
【问题描述】:

我可以通过使用以下代码将缓冲区数据附加到音频文件的 END 来将音频录制到音频文件:

// AVAudioFile uses the Core Audio Format (CAF) to write to disk.
// So we're using the caf file extension.
file = try AVAudioFile(forWriting: FileManagerHelper.recordingLocalURL(), settings: format.settings)

tapNode.installTap(onBus: 0, bufferSize: bufferSize, format: format, block: {
  (buffer, _) in
  do {
    buffer.frameLength = 1024 // Tap is now called every 40ms. By default, tap is called every 0.375s
    try self.file?.write(from: buffer)
    self.delegate?.recorderDidRecieveAudioBuffer(self, buffer: buffer)
    semaphore.signal()
  } catch {
    log("Error writing mic data to file.", msgType: .error)
  }
})

但是有没有办法将缓冲区数据写入文件的某个位置,即缓冲区数据中的时间或索引?

谢谢

【问题讨论】:

    标签: ios swift avaudioengine


    【解决方案1】:

    根据苹果文档:

    var framePosition: AVAudioFramePosition: 文件中将发生下一次读取或写入操作的位置。

    因此,您只需将 framePosition 设置为您想要开始录制新音频数据的帧中的任何位置。

    【讨论】:

    • 我可能错了,但据我记得这会覆盖现有数据。这真的是你想要完成的事情吗?
    • 是的,就像录音机在语音备忘录中的工作方式一样
    • 但是,设置 framePosition 似乎会导致“错误 -66568”。不知道这是什么意思
    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2013-10-26
    • 2020-01-26
    • 2011-08-19
    相关资源
    最近更新 更多