xcode - 阻止音频文件相互播放

xcode - stop audio files from playing over each other

所以我是 xcode 和一般编程的新手。我正在创建一个简单的音板,但 运行 遇到了问题。我想要做的是在选择不同的音频按钮时停止播放当前音频。然后让停止的音频自行重置,并准备好在再次按下时从头开始播放。我在我的代码中添加了一个 stop() ,它会停止播放音频,但是当我尝试重新启动它时,我发现音频刚刚暂停,并从停止的地方继续。

任何人都可以向我提供修复此问题所需的代码集吗?如果没有,任何人都可以为我指明可以引导我走上正确道路的良好教程的方向。看了apple Audio的文档,可惜迷路了

我的代码如下

var SoundPlayer1:AVAudioPlayer = AVAudioPlayer()
var SoundPlayer2:AVAudioPlayer = AVAudioPlayer()
var SoundPlayer3:AVAudioPlayer = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()
    let FileLocation1 = Bundle.main.path(forResource: "Audio1", ofType: "mp3")
    let FileLocation2 = Bundle.main.path(forResource: "Audio2", ofType: "mp3")
    let FileLocation3 = Bundle.main.path(forResource: "Audio3", ofType: "mp3")

    do {
        SoundPlayer1 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation1!))
        SoundPlayer2 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation2!))
        SoundPlayer3 = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: FileLocation3!))

        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
        try AVAudioSession.sharedInstance().setActive(true)
    }

    catch {
        print(error)
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func catButton(_ sender: AnyObject) {
    SoundPlayer1.play()
}

@IBAction func pigButton(_ sender: AnyObject) {
    SoundPlayer2.play()
}

@IBAction func pigButton(_ sender: AnyObject) {
    SoundPlayer3.play()
}

这只是一些 Swift 逻辑,您可以执行这些逻辑来检查是否有其他玩家正在玩,然后阻止他们!

  // Make sure all audio playback times are reset
  func resetAllAudioTime(){
    SoundPlayer1.currentTime = 0.0;
    SoundPlayer2.currentTime = 0.0;
    SoundPlayer3.currentTime = 0.0;
  }

  IBAction func catButton(_ sender: AnyObject) {

  // Check SoundPlayer2 and stop if playing 
  if(SoundPlayer2.playing){
    SoundPlayer2.pause();
  }

  // Check SoundPlayer3 and stop if playing
  if(SoundPlayer3.playing){
    SoundPlayer3.pause();
  }

  // Reset all playback times to start of file
  resetAllAudioTime();

  // Play audio file
  SoundPlayer1.play()
}

@IBAction func pigButton(_ sender: AnyObject) {

  // Check SoundPlayer1 and stop if playing 
  if(SoundPlayer1.playing){
    SoundPlayer1.pause();
  }

  // Check SoundPlayer3 and stop if playing
  if(SoundPlayer3.playing){
    SoundPlayer3.pause();
  }

  // Reset all playback times to start of file
  resetAllAudioTime();

  // Play audio file
  SoundPlayer2.play()
}

@IBAction func pigButton(_ sender: AnyObject) {

  // Check SoundPlayer1 and stop if playing 
  if(SoundPlayer1.playing){
    SoundPlayer1.pause();
  }

  // Check SoundPlayer2 and stop if playing
  if(SoundPlayer2.playing){
    SoundPlayer2.pause();
  }

  // Reset all playback times to start of file
  resetAllAudioTime();

  // Play audio file
  SoundPlayer3.play()
}

注意: 此代码未经测试,但应该可以使用。请参阅 AVAudioPlayer 文档以获得更多帮助。

您会看到我没有调用 AVAudioPlayer.stop() 方法,而是使用了 AVAudioPlayer.pause() 然后将播放时间重置为开始。如果您阅读 documentation,它指出调用 AVAudioPlayer.stop() 方法:

func stop() 

Stops playback and undoes the setup needed for playback.

因此,调用暂停和重置时间并不会一直撤消播放所需的设置(应该更有效!)。

希望对您有所帮助! :-)

WoodyDev,感谢您提供清晰易懂的信息。我能够通过两个函数解决我的问题。

    func stopAll() {
        SoundPlayer1.stop()
        SoundPlayer2.stop() 
}   
    func resetAudioTime() {
        SoundPlayer1.currentTime = 0.0;
        SoundPlayer2.currentTime = 0.0; 
}    
    @IBAction func catButton(_ sender: AnyObject) {
        stopAll();<br/>
        resetAudioTime();<br/>
        SoundPlayer1.play()
}