我正在将使用 AVPlayer 的 react-native 包重构为 AVAudioEngine 和 AVAudioPlayerNode。但是我很困惑如何使用 AVAudioPlayerNode 来打开 AVPlayer.addPeriodicTimeObserver。这个想法是我需要在播放音乐时获取时间数据,因此在这个 addPeriodicTimeObserver 闭包中需要发送间隔,所以在这方面如何在 AVAudioPlayerNode 上使用它,谢谢!
func addPeriodicTimeObserver() {
let timeScale = CMTimeScale(NSEC_PER_SEC)
let time = CMTime(seconds: subscriptionDuration, preferredTimescale: timeScale)
timeObserverToken = audioPlayer.addPeriodicTimeObserver(forInterval: time,
queue: .main) {_ in
if (self.audioPlayer != nil) {
self.sendEvent(withName: "rn-playback", body: [
"isMuted": self.audioPlayer.isMuted,
"currentPosition": self.audioPlayerItem.currentTime().seconds * 1000,
"duration": self.audioPlayerItem.duration.seconds * 1000,
])
}
}
}