gpt4 book ai didi

swift - 如何延迟 For 循环直到声音在 Swift 中播放完毕

转载 作者:可可西里 更新时间:2023-11-01 01:37:25 24 4
gpt4 key购买 nike

因此,我有一个 for 循环,它将数组中的值运行到 if 语句中。 if 语句根据数组中的值播放声音。

然而,现在,所有的值都在一次运行,所以所有的声音都是同时播放的。这是我的代码现在的样子:

// sortedKeys is an array made from the keys in the newData dictionary.
let sortedKeys = Array(newData.keys).sort(<)

// newData is the dictionary of type [float:float] being used to get the values that are then being run through the if statement.
for (value) in sortedKeys {
let data = newData[value]
if data <= Float(1) {
self.audioPlayer1.play()
} else if data <= Float(2) && data > Float(1) {
self.audioPlayer2.play()
} else if data <= Float(3) && data > Float(2) {
self.audioPlayer3.play()
} else if data <= Float(4) && data > Float(3) {
self.audioPlayer4.play()
} else if data <= Float(5) && data > Float(4) {
self.audioPlayer5.play()
} else if data <= Float(6) && data > Float(5) {
self.audioPlayer6.play()
} else if data <= Float(7) && data > Float(6) {
self.audioPlayer7.play()
} else if data <= Float(8) && data > Float(7) {
self.audioPlayer8.play()
} else if data <= Float(9) && data > Float(8) {
self.audioPlayer9.play()
} else {
self.audioPlayer10.play()
}
}

我怎样才能让它在 AVAudioPlayer 完成播放后继续 for 循环以获得下一个声音?我认为它与 AVPlayerItemDidPlayToEndTimeNotification 有关,但我不知道如何使用它来延迟 for 循环。

谢谢!

最佳答案

How can I make it so that once the AVAudioPlayer finishes playing, then I continue the for loop to get the next sound.

让音频播放器的委托(delegate)实现 -audioPlayerDidFinishPlaying:successfully: 以便它选择下一个声音并播放它。

音频是异步播放的,因此您不能让代码在声音播放时暂停一下然后继续播放。您需要设计您的代码,使其触发声音,然后当音频播放器说它结束时,开始播放下一个声音。

关于swift - 如何延迟 For 循环直到声音在 Swift 中播放完毕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249281/

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