gpt4 book ai didi

ios - 未调用 Swift audioPlayerDidFinishPlaying 方法

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

我在 MainStoryBoard 上创建了三个 UIButton。

当我按下“yourButton3”时,不会调用 player3。

我发现更改 else if 子句或将某些内容分配给 player3 可以解决此问题,但不知道如何解决。

       override func viewDidLoad() {
super.viewDidLoad()
player1?.delegate = self
player2?.delegate = self
player3?.delegate = self
}

let url1 = Bundle.main.bundleURL.appendingPathComponent("music1.mp3")
let url2 = Bundle.main.bundleURL.appendingPathComponent("music2.mp3")

@IBOutlet weak var yourButton: customButton!
@IBOutlet weak var yourButton2: customButton!
@IBOutlet weak var yourButton3: customButton!


fileprivate var player1:AVAudioPlayer?
fileprivate var player2:AVAudioPlayer?
fileprivate var player3:AVAudioPlayer?


@IBAction func pushButton1(sender: UIButton) {
Player(url: url1)
}

@IBAction func pushButton2(sender: UIButton) {
Player1(url: url2)
}

@IBAction func pushButton3(_ sender: UIButton) {
Player(url: url2)
player1!.play()

}

//player3 is not called in this code

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



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

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

} catch {
print(error)
}
}

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

最佳答案

您还没有在 Player(url:) 函数中初始化 player3,因此 player === player3 永远不会为真。

您可以编辑您的 Player3(url:, url2:) 函数,如下所示:

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

然后这样调用它:

@IBAction func pushButton3(_ sender: UIButton) {
Player3(url: url1, url2: url2)
}

关于ios - 未调用 Swift audioPlayerDidFinishPlaying 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265822/

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