I use AVAudioPlayer to play a set of synchronized MP3 files in the player style interface (Play / Pause / Stop). I know that I can synchronize them using play (atTime :) , but my question is: how do I synchronize their suspension? Currently, to pause them, I use:
for (_, audioTrackPlayer) in audioTrackPlayers.enumerated() {
audioTrackPlayer.pause()
}
But, of course, they all stop one after another. There is no equivalent pause (atTime :). So, how to achieve accurate timing to pause?
Edit - Additional Information
Following the matte query about how noticeable this is, I got some data about this. It takes almost half a second to pause 5 tracks. To show this, I added a separate cycle after the pause cycle to report the current time on each track, and also added a couple of cycles before pausing — for reference, and to show that they were synchronized to begin with.
Loop benchmark, 0: 3.2118820861678
Loop benchmark, 0: 3.21580498866213
Loop benchmark, 0: 3.21888888888889
Loop benchmark, 0: 3.22126984126984
Loop benchmark, 0: 3.22328798185941
Before pause, 0: 3.22560090702948
Before pause, 1: 3.22750566893424
Before pause, 2: 3.22975056689342
Before pause, 3: 3.23185941043084
Before pause, 4: 3.23439909297052
After pause, 0: 3.35040816326531
After pause, 1: 3.46598639455782
After pause, 2: 3.56979591836735
After pause, 3: 3.67455782312925
After pause, 4: 3.77895691609977
Thus, the tracks were synchronized until a pause, and the time difference was only due to 3 or 4 milliseconds that are required for each iteration of the loop. After a pause, there was about one tenth of a second between each track.
Code for reference:
for _ in 0..<5 {
print ("Loop benchmark, 0: \(audioTrackPlayers[0].currentTime)")
}
for (index, audioTrackPlayer) in audioTrackPlayers.enumerated() {
print ("Before pause, \(index): \(audioTrackPlayer.currentTime)")
}
for audioTrackPlayer in audioTrackPlayers {
audioTrackPlayer.pause()
}
for (index, audioTrackPlayer) in audioTrackPlayers.enumerated() {
print ("After pause, \(index): \(audioTrackPlayer.currentTime)")
}