0

During the scrolling of my app feed, I load some videos inside an AVPlayer. The problem is that AVPlayerItem playerItemWithUrl and AVPlayer playerWithPlayerItem are a little slow (around 16ms), so it's produce frame drop in my app. Is their any way to speed it up? Maybe i can call these functions in a background thread ?

Below the code i use to create the Player:

  FPlayerItem := TAVPlayerItem.Wrap(TAVPlayerItem.OCClass.playerItemWithURL(aURL)); 
  FPlayer := TAVPlayer.Wrap(TAVPlayer.OCClass.playerWithPlayerItem(FPlayerItem)); 
zeus
  • 12,173
  • 9
  • 63
  • 184
  • Can you share the code you have used to play the item ? – Nitish Feb 01 '18 at 19:22
  • It's will be probably irrelevant because it's made in delphi, but i simply do AVPlayerItem playerItemWithUrl and later AVPlayer playerWithPlayerItem nothing else – zeus Feb 01 '18 at 20:03
  • Did you try playing the item inside loadValuesAsynchronously ? – Nitish Feb 01 '18 at 20:07
  • i update the question with the code. no, how to use loadValuesAsynchronously ? – zeus Feb 01 '18 at 20:15

1 Answers1

1

Play the item inside AVAsynchronousKeyValueLoading.
It clearly states :

Methods you can implement to use an asset or asset track without blocking the calling thread.

So logically, for understanding:

let asset = AVAsset(url: url)
let keys: [String] = ["playable"]
asset.loadValuesAsynchronously(forKeys: keys) {
DispatchQueue.main.async {         
      let item = AVPlayerItem(asset: asset)
      let player = AVPlayer(playerItem: item)
      player.play()
      }
}  

This answer will help you understand it better.

Nitish
  • 13,845
  • 28
  • 135
  • 263