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

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

最佳答案

这只是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.


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

关于ios - xcode-阻止音频文件相互播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275967/

相关文章:

ios - 如何在按下回车键时 swift 隐藏键盘?

ios - objective-c 中的方法顺序

iphone - 引用 AppDelegate 方法 - iPhone

iphone - 如何在Xcode中用+替换空格

java - 将 16 位 pcm 转换为 8 位

java - Android/Java 混音和静音

ios - Alamofire 5.0+ MultipartFormData 上传不记名 token

ios - UILabel动态获取字体大小 - swift 3

ios - 在 Storyboard 中复制 UIView

android - 如何检测音频文件何时播放完毕?