我有一个动画菜单来启动一些视频。该视频实现为一个skVideoNode。当我第一次按下菜单按钮时,菜单的动画以及视频开始被延迟。我认为必须加载一些东西,所以我预装了所有纹理。同样,在经过一些测试后,它会在视频启动的地方苛刻。可能是什么原因。
我在程序启动时初始化了avPlayer,然后我只更改了项目。动画菜单会在之前触发,但也会停止一秒钟。为什么呢?
func setupVideo() {
let fm = FileManager.default
let urls = fm.urls(for: .documentDirectory, in: .userDomainMask)
// if let url = urls.first as? URL {
if let url = urls.first {
let urlPath : String = url.path + "/" + "load.mp4"
let item = AVPlayerItem(url: URL(fileURLWithPath: urlPath) )
let vPlayer = AVPlayer(playerItem: item)
avPlayer = vPlayer
}
}
使用菜单按钮
触发 if let url = urls.first {
let urlPath : String = url.path + "/" + videoName
let item = AVPlayerItem(url: URL(fileURLWithPath: urlPath) )
avPlayer.replaceCurrentItem(with: item)
let timeInterval: CMTime = CMTimeMakeWithSeconds(0.5, 10)
avPlayerTimeObserver = avPlayer.addPeriodicTimeObserver(forInterval: timeInterval, queue: DispatchQueue.main) { (elapsedTime: CMTime) -> Void in
let floatTime = Float(CMTimeGetSeconds(elapsedTime))
// print("elapsedTime now:", CMTimeGetSeconds(elapsedTime) )
self.avPlayerObserveTime(floatTime: floatTime)
} as AnyObject
}