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

时间:2018-07-11 01:25:23

标签: ios xcode audio avaudioplayer

因此,我一般对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()
}

2 个答案:

答案 0 :(得分:0)

这只是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() 
     

停止播放并撤消播放所需的设置。

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

我希望这会有所帮助! :-)

答案 1 :(得分:0)

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()
}