title: Maintaining good scroll performance when using AVPlayer
body: <p>I'm working on an application where there is a collection view, and cells of the collection view can contain video. Right now I'm displaying the video using <code>AVPlayer</code> and <code>AVPlayerLayer</code>. Unfortunately, the scrolling performance is terrible. It seems like <code>AVPlayer</code>, <code>AVPlayerItem</code>, and <code>AVPlayerLayer</code> do a lot of their work on the main thread. They are constantly taking out locks, waiting on semaphores, etc. which is blocking the main thread and causing severe frame drops.</p>
  
  <p>Is there any way to tell <code>AVPlayer</code> to stop doing so many things on the main thread? So far nothing I've tried has solved the problem.</p>
  
  <p>I also tried building a simple video player using <code>AVSampleBufferDisplayLayer</code>. Using that I can make sure that everything happens off the main thread, and I can achieve ~60fps while scrolling and playing video. Unfortunately that method is much lower level, and it doesn't provide things like audio playback and time scrubbing out of the box. Is there any way to get similar performance with <code>AVPlayer</code>? I'd much rather use that.</p>
  
  <p><strong>Edit:</strong>
  After looking into this more, it doesn't look like it's possible to achieve good scrolling performance when using <code>AVPlayer</code>. Creating an <code>AVPlayer</code> and associating in with an <code>AVPlayerItem</code> instance kicks off a bunch of work which trampolines onto the main thread where it then waits on semaphores and tries to acquire a bunch of locks. The amount of time this stalls the main thread increases quite dramatically as the number of videos in the scrollview increases.</p>
  
  <p><code>AVPlayer</code> dealloc also seems to be a huge problem. Dealloc'ing an <code>AVPlayer</code> also tries to synchronize a bunch of stuff. Again, this gets extremely bad as you create more players.</p>
  
  <p>This is pretty depressing, and it makes <code>AVPlayer</code> almost unusable for what I'm trying to do. Blocking the main thread like this is such an amateur thing to do so it's hard to believe Apple engineers would've made this kind of mistake. Anyways, hopefully they can fix this soon.</p>