iOS AVPlayer won't play short video in iPhone 7

Viewed 94

I am trying to play mp4 video file continuously. But it is not played short video about 2 seconds. With videos more than 2 seconds(6 seconds) is worked well. It is happened only in iPhone 7 in device and simulator. But with other devices it is worked well also. My code is here:

//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()
0 Answers
Related