微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS AVPlayer 无法在 iPhone 7 中播放短视频

如何解决iOS AVPlayer 无法在 iPhone 7 中播放短视频

我正在尝试连续播放 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 举报,一经查实,本站将立刻删除。