dimanche 7 février 2016

If statement Obstructs Sound Delay

So, I'm trying to play two sounds, one after the other, using a delay (playAtTime). It works perfectly when I just do this:

let duration : NSTimeInterval = audioPlayer1.duration
var playTime : NSTimeInterval = audioPlayer2.deviceCurrentTime + duration

for (value) in sortedKeys {
        let xPoint = imageData[value]

        if xPoint <= Float(1) {

            self.audioPlayer1.stop()
            self.audioPlayer1.currentTime = 0
            self.audioPlayer1.play()

            self.audioPlayer2.stop()
            self.audioPlayer2.currentTime = 0
            self.audioPlayer2.playAtTime(playTime)
        }

However, when I separate the code into an if-statement, it only plays the first sound.

for (value) in sortedKeys {
        let xPoint = imageData[value]

        if xPoint <= Float(1) {

            self.audioPlayer1.stop()
            self.audioPlayer1.currentTime = 0
            self.audioPlayer1.play()

        } else {

            self.audioPlayer2.stop()
            self.audioPlayer2.currentTime = 0
            self.audioPlayer2.playAtTime(playTime)

        }

The for loop makes it so that both conditions of the if-statement are met - eventually, both blocks of code are run. Obviously the issue is that the two blocks of code must be run next to each other, and not as two parts of an if-statement. Anyone know why that is, and how I can fix it?

Thanks!

Aucun commentaire:

Enregistrer un commentaire