The problem is as follows:
Rhythm games require a lot of action related to the rhythm of the music, and in order to maintain the rhythm, it is necessary to have a close relationship with the beat. The interval requirement cannot exceed 0.02 seconds or less.
The Asyncafter (Deadline:) method in GCD is currently used, but the error is always greater than 0.05 seconds, and there is no guarantee that the error will pass. Is there a better way to solve the error?
var time = Date().timeIntervalSince1970let dq = DispatchQueue(label: "queue", qos: .userInitiated, attributes: [.concurrent, .initiallyInactive]) dq.async { self.audioPlayer.play() time = Date().timeIntervalSince1970 } dq.asyncAfter(deadline: .now() + 0.43, execute: { let res = Date().timeIntervalSince1970 print(" 误差: \(res - time)") })
The following is a multiple-run console print result (this number refers to the actual interval, which is far from 0.43 of the expected interval and is not stable):
IOS: 0 timing or delayed execution of error or minimum error? >> iOS
The answer is quite clear:
Http://www.goodpm.net/postreply/ios/1010000008928622/iOS 0 Timing or delay execution of error or minimum error. html
IOS: 0 timing or delayed execution of error or minimum error?