gpt4 book ai didi

ios - 如何在一个类中使用 audioPlayerDidFinishPlaying

转载 作者:行者123 更新时间:2023-11-28 15:51:44 25 4
gpt4 key购买 nike

Player1 运行良好,但我在执行 Player2 时出错。

我应该如何更改 audioPlayerDidFinishPlaying 以使其工作?

这是我的 ViewController.swift 的代码:

  func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if (player === player2) {
yourButton2.isSelected = false
} else {
     yourButton.isSelected = false
player2!.play()
yourButton2.isSelected = false
}
}


func Player1(url: URL) {
do {
try player2 = AVAudioPlayer(contentsOf:url)
player2!.play()
yourButton2.isSelected = true
player2!.delegate = self

} catch {
print(error)
}
}

func Player2(url: URL, url2: URL) {
do {
try player1 = AVAudioPlayer(contentsOf:url)
try player2 = AVAudioPlayer(contentsOf: url2)
player1!.play()
yourButton.isSelected = true
player1!.delegate = self
} catch {
print(error)
}
}

最佳答案

audioPlayerdidFinishPlaying(_:successfully:) 中,您有一个 else if 子句永远不会变为 true:

} else if (player === player3) {

player3始终nil 因为您永远不会为其分配任何内容。因此,您在 audioPlayerdidFinishPlaying(_:successfully:) 中的第 3 个分支将永远不会被执行。要在您当前的类(class)设计中实现此功能,您需要:

  • 更改else if 子句
  • 分配一些东西给player3

关于ios - 如何在一个类中使用 audioPlayerDidFinishPlaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42212595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com