问题
I've got two AVAudioPlayerNodes.
I schedule one using scheduleBuffer.
I want to cancel it before it starts but not if it has already started.
How do I know if it has actually started?
The isPlaying flag appears to be set as soon as it is scheduled rather than when it actually starts at the scheduled time.
回答1:
AVAudioPlayerNode takes a render cycle after calling play()
before actual audio starts playing. If you are trying to identify the time between play()
and audio being played, you can get the playerNode's playerTime and look for negative (or less than specified with play(at:)
) sampleTime values. But since you're on different threads than the audio being scheduled, there's still a chance that a render cycle's worth of audio can squeeze out before you stop the player.
let playerTime = playerNode.playerTime(forNodeTime: AVAudioTime.init(hostTime: mach_absolute_time()))
print("sample time \(playerTime.sampleTime)")
来源:https://stackoverflow.com/questions/44789866/is-avaudioplayernode-isplaying-flag-set-as-soon-as-schedulebuffer-is-called