问题描述
我正在尝试连续播放 mp4 视频文件。但它没有播放约2秒的短视频。超过 2 秒(6 秒)的视频效果很好。它只发生在 iPhone 7 的设备和模拟器中。但是对于其他设备,它也运行良好。 我的代码在这里:
//Stop player before starting new play
player?.pause()
player?.replaceCurrentItem(with: nil)
player = nil
playerLayer?.removeFromSuperlayer()
playerLayer = nil
guard let url = Bundle.main.url(forResource: video,withExtension: "mp4") else {
return
}
try? AVAudioSession.sharedInstance().setCategory(.playAndRecord)
try? AVAudioSession.sharedInstance().setActive(true)
player = AVPlayer(url: url)
player?.volume = 1.0
playerLayer = AVPlayerLayer(player: player)
videoContainerView.layer.addSublayer(playerLayer)
playerLayer?.frame = videoContainerView.bounds
//track player progress
let interval = CMTime(value: 1,timescale: 2)
player?.addPeriodicTimeObserver(forInterval: interval,queue: .main,using: { [weak player] (progresstime) in
let seconds = CMTimeGetSeconds(progresstime)
guard let duration = player?.currentItem?.duration else {
return
}
let durationSeconds = CMTimeGetSeconds(duration)
if seconds >= durationSeconds {
// Seek and play again
player?.seek(to: CMTime.zero,completionHandler: { (completedSeek) in
player?.play()
})
}
})
player?.play()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)