Player1运行良好,但我在实现Player2时出错。
我应该如何更改音频播放器IDfinishplaying以使其工作?
这是my 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:)
中的第三个分支将永远不会被执行。要在当前的类设计中实现此功能,您需要:更改
else if
子句分配给
player3
关于ios - 如何在一类中使用audioPlayerDidFinishPlaying,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42212595/