0

我有这个按钮来播放和循环一个 wav。但是,如果我有另一个带有另一个循环的按钮怎么办?我希望第二个循环一旦按下就开始播放,但是,第一个循环必须完成他的“回合”。反之亦然(播放并循环第二个循环,按下按钮第一个循环并接管)

 @IBAction func playButtonTapped(_ sender: Any) {



    guard let filePath: String = Bundle.main.path(forResource: "25loop110", ofType: "wav") else{ return }
          print("\(filePath)")
          let fileURL: URL = URL(fileURLWithPath: filePath)
          guard




              let audioFile = try? AVAudioFile(forReading: fileURL) else{ return }

          let audioFormat = audioFile.processingFormat
          let audioFrameCount = UInt32(audioFile.length)
          guard let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)  else{ return }
          do{
              try audioFile.read(into: audioFileBuffer)

              timeShift.rate = adjustedBpm/bpm
              playerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)


          } catch{
              print("over")
          }

          try? audioEngine.start()
          playerNode.play()
          playerNode.scheduleBuffer(audioFileBuffer, at: nil, options:.loops,completionHandler: nil)

    }
4

2 回答 2

1

您可以使用 的completionHandler参数来处理此行为.scheduleBuffer

例如,您可以执行以下操作:

var nextAudioFilePath: String  
var isPlaying: Bool = false

@IBAction func playLoopA() {  
   guard let path = Bundle.main.path(forResource: "audioFileA", ofType: "wav") else { return }  
   nextAudioFilePath = path  
   guard !isPlaying else { return }  
   play()
}  

@IBAction func playLoopB() {  
   guard let path = Bundle.main.path(forResource: "audioFileB", ofType: "wav") else { return }  
   nextAudioFilePath = path  
   guard !isPlaying else { return }  
   play()
}  

private func play() {  
   let fileURL = URL(fileURLWithPath: nextAudioFilePath)  
   ...  
   playerNode.scheduleBuffer(audioFileBuffer, at: nil, options: [], completionHandler: { [weak self] in   
      self?.play()
   })
}  
于 2020-02-09T14:25:31.683 回答
0

我还找到了这个解决方案:

playerNode.scheduleBuffer(audioFileBuffer, at: nil, options:[.interruptsAtLoop, .loops],completionHandler: nil)
于 2020-02-09T18:58:51.417 回答