When selecting a row the following code sets up a set of AVAudioPlayer
s to playback at a certain date (in this case, 50 players playing in the interval of 1 second).
Since I want the whole process to restart when touching again I need to break the setup in the for loop since it takes a few seconds to setup the players.
Apart from that, each player is being removed after finishing playback using the audioDidFinishPlaying
delegate method of AVAudioPlayerDelegate
. I did not include this in the code since it is not relevant to the question.
I've tried using a flag inside the for loop to check whether setup is allowed but that doesn't work.
var players: [AVAudioPlayer] = []
var loadError: NSError?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Removes the players that have been setup already.
players.removeAll()
// The for loop from the previous row selection should be stopped here.
for i in 0..<50 {
do {
let player = try AVAudioPlayer(contentsOfURL: soundUrls[i])
players += [player]
// The process of setting these up takes a few seconds, I need to break it.
print("Firing timer")
player.playAtTime(player.deviceCurrentTime + NSTimeInterval(i))
} catch let error as NSError {
loadError = error
}
}
}
What happens is, that the setup triggered by the previous row selection will continue until it is finished and only then the new for loop starts. I need to break it earlier.
I can't figure out how to tackle this. Maybe by removing the processes from the main thread(How?)? Any help much appreciated!