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 AVPlayer
and AVPlayerLayer
. Unfortunately, the scrolling performance is terrible. It seems like AVPlayer
, AVPlayerItem
, and AVPlayerLayer
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.
Is there any way to tell AVPlayer
to stop doing so many things on the main thread? So far nothing I've tried has solved the problem.
I also tried building a simple video player using AVSampleBufferDisplayLayer
. 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 AVPlayer
? I'd much rather use that.
Edit:
After looking into this more, it doesn't look like it's possible to achieve good scrolling performance when using AVPlayer
. Creating an AVPlayer
and associating in with an AVPlayerItem
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.
AVPlayer
dealloc also seems to be a huge problem. Dealloc'ing an AVPlayer
also tries to synchronize a bunch of stuff. Again, this gets extremely bad as you create more players.
This is pretty depressing, and it makes AVPlayer
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…