0

我有一个从本机库获得的原始音频字节流,为了使它工作,我将它转换为AVAudioPCMBuffer我可以AVAudioEngine像这样使用的

...
        player.scheduleBuffer(pcmBuf, completionHandler: nil)
        if !player.isPlaying { player.play() }
...

但问题是 - 如何进行搜索(例如:+/-10 秒)。我在这里找到了解释

https://www.raywenderlich.com/21672160-avaudioengine-tutorial-for-ios-getting-started

  private func seek(to time: Double) {
    guard let audioFile = audioFile else {
      return
    }

    // 1
    let offset = AVAudioFramePosition(time * audioSampleRate)
    seekFrame = currentPosition + offset
    seekFrame = max(seekFrame, 0)
    seekFrame = min(seekFrame, audioLengthSamples)
    currentPosition = seekFrame

    // 2
    let wasPlaying = player.isPlaying
    player.stop()

    if currentPosition < audioLengthSamples {
      updateDisplay()
      needsFileScheduled = false

      let frameCount = AVAudioFrameCount(audioLengthSamples - seekFrame)
      // 3
      player.scheduleSegment(
        audioFile,
        startingFrame: seekFrame,
        frameCount: frameCount,
        at: nil
      ) {
        self.needsFileScheduled = true
      }

      // 4
      if wasPlaying {
        player.play()
      }
    }
  }

但是给定示例的问题是没有scheduleSegment使用startingFrameparam for 的方法AVAudioPCMBuffer

有没有办法实现搜索逻辑AVAudioPCMBuffer

4

0 回答 0