There exists a way to pause AVAudioPlayer at the same time? (Similar to player.play(atTime: TimeInterval))

Viewed 14

I am developing an App in which the user can play different sounds simultaneously, and I do it with AVAudioPlayer using play(atTime), but I pause the sounds in a loop and then play again the audio will play with a delay between them.

The way I am pausing audio is:

var players: [AVAudioPlayer] = []

for player in players {
            player.pause()
        }

But it does not stop all the audios at the same time, so when I play it again, it starts with a delay between audios.

1 Answers

I just figured it out:

I set the player's currentTime to the same for all of them:

func pauseTracks() {
    if let firstPlayer = players.first {
        let currentPosition = firstPlayer.currentTime
        for player in players {
            player.pauseTrack()
            player.currentTime = currentPosition
        }
    }
}
Related