我知道 player.currentItem.currentTime().seconds
返回正在播放的音频文件的当前时间(以秒为单位) .
我可以在 player
上添加一个每毫秒调用一次的回调函数吗?
例如,对于第二个1,此callBack函数应调用1000次 .
问题是使用 addPeriodicTimeObserver
只能每秒调用一次callBack函数,而不是每秒1000次 .
Why do I need this?
1.
我正在播放音频文件时通过点击UI中的按钮手动提取萨尔萨歌曲的节拍数,并将其记录为 [Int:Int]
// milisecondButtonTapped:beatCount .
我的阵列看起来像var beatCounts = [982: 1, 2051: 2, 3006: 3, 5027: 5, 6011: 6, 7028: 7]
音频播放结束后,我将得到一个包含beatCounts的数组,该数字对应于某些毫秒 player.currentItem.currentTime()
2.
现在,我想再次播放这首歌,并检查播放器currentTime以毫秒为单位==对于 var beatCounts
中的一个值
func checkCurrentTime(playerTimeMilliseconds: Int) {
for (millisecond, beatCount) in beatCounts {
if playerTimeMilliseconds == millisecond {
//show beatCount in label on UI
}
}
}
func playSound(url: URL) {
let playerItem: AVPlayerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try! AVAudioSession.sharedInstance().setActive(true)
player?.play()
player?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1000), queue: DispatchQueue.main, using: { [weak self] (CMTime) in
print("cmtime is \(CMTime)")
CMTime.value
if self?.player?.currentItem?.status == .readyToPlay {
}
})
}//end playSound