ttr*_*trs 5 ios avplayer swift
我需要在开始播放后立即跳转到音频文件中的特定时间。所以我使用seekToTime方法和完成处理程序
avPlayer.play()
Run Code Online (Sandbox Code Playgroud)
...
avPlayer?.seekToTime(jumpTime, completionHandler: { isComplete in
if isComplete {
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = CMTimeGetSeconds((self.avPlayer!.currentItem?.currentTime())!)
}
})
Run Code Online (Sandbox Code Playgroud)
问题是它需要时间来开始从互联网播放文件。出于某种原因,带有完成处理程序的 seekToTime 版本使应用程序崩溃,因为它在 avPlayer 开始播放之前被调用。没有完成处理程序的版本工作正常。
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'AVPlayerItem cannot service a seek request with a completion handler until its status is AVPlayerItemStatusReadyToPlay.'
Run Code Online (Sandbox Code Playgroud)
有没有办法使用回调avPlayer.play()?
是的,有办法。
您需要观察玩家的变化:
avPlayer?.addPeriodicTimeObserverForInterval(CMTime(value: 1, timescale: 3), queue: dispatch_get_main_queue()) { [weak self] time in
self?.handleCallback(time)
}
Run Code Online (Sandbox Code Playgroud)
然后你在回调中处理状态:
func handleCallback(time: CMTime) {
if avPlayer?.status == .ReadyToPlay {
// place your seek logic here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4660 次 |
| 最近记录: |