So, I have a for loop that runs values from an array into an if statement. The if statement plays a sound depending on the value in the array.
我有一个for循环,它将数组中的值运行到if语句中。if语句根据数组中的值播放声音。
However, right now, all the values are being run through at once, so the sounds are all played at the same time. Here's what my code looks like right now:
然而,现在,所有的值都同时运行,所以所有的声音都同时播放。下面是我现在的代码:
// 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()
}
}
How can I make it so that once the AVAudioPlayer
finishes playing, I continue the for loop to get the next sound? I'm thinking it has something to do with AVPlayerItemDidPlayToEndTimeNotification
, but I don't know how to use this to delay the for loop.
如何使AVAudioPlayer完成后,继续for循环以获取下一个声音?我认为这与AVPlayerItemDidPlayToEndTimeNotification有关,但是我不知道如何使用它来延迟for循环。
Thanks!
谢谢!
1 个解决方案
#1
2
How can I make it so that once the AVAudioPlayer finishes playing, then I continue the for loop to get the next sound.
如何使AVAudioPlayer完成后,继续for循环以获取下一个声音。
Have the audio player's delegate implement -audioPlayerDidFinishPlaying:successfully:
such that it selects the next sound and plays it.
使音频播放器的委托实现-audioPlayerDidFinishPlaying:成功:使它选择下一个声音并播放它。
Audio is played asynchronously, so you can't just have your code pause for a bit while the sound plays and then continue on. You need to design your code so that it fires off the sound, and then when the audio player says it's finished, the next sound is started.
音频是异步播放的,因此您不能让您的代码在声音播放时暂停片刻,然后继续。你需要设计你的代码,让它发出声音,然后当音频播放器说它完成时,下一个声音就开始了。
#1
2
How can I make it so that once the AVAudioPlayer finishes playing, then I continue the for loop to get the next sound.
如何使AVAudioPlayer完成后,继续for循环以获取下一个声音。
Have the audio player's delegate implement -audioPlayerDidFinishPlaying:successfully:
such that it selects the next sound and plays it.
使音频播放器的委托实现-audioPlayerDidFinishPlaying:成功:使它选择下一个声音并播放它。
Audio is played asynchronously, so you can't just have your code pause for a bit while the sound plays and then continue on. You need to design your code so that it fires off the sound, and then when the audio player says it's finished, the next sound is started.
音频是异步播放的,因此您不能让您的代码在声音播放时暂停片刻,然后继续。你需要设计你的代码,让它发出声音,然后当音频播放器说它完成时,下一个声音就开始了。