IOS AVPlayer replaceCurrentItemWithPlayerItem: nil block UI Thread

It seems that api replaceCurrentItemWithPlayerItem: the main thread stuck for a few seconds, I understand that replacing an element requires information about a new element, which may take some time to preload. But questions arise, why replaceCurrentItemWithPlayerItem: with nil item object would the main thread also get stuck? It happens to me that sometimes it takes more than 5 seconds to replace nil playerItem. I wonder what I can do to avoid the problem. Thanks for any tips!

+6
source share
1 answer

I ran into a similar UI blocking problem when I used UICollectionView to display and preview videos in a local photo library through ALAssetLibrary .

Scrolling while switching a video does not work smoothly, so I assume some element is blocking the user interface stream. Then I use Core Animation for Instruments to analyze what exactly the user interface thread occupies. In Time Profiler I found that replaceCurrentItemWithPlayerItem takes about 30 ms to execute in the main thread, which is more than 16 ms (1000/60 (fps)) leads to intermittent scrolling.

To solve the problem, I first tried to put replaceCurrentItemWithPlayerItem in the background thread using GCD, but it does not work. I'm not sure if this is because Cocoa itself needs to update the interface when calling replaceCurrentItemWithPlayerItem , which means the UI thread is still blocked. Finally, I started it by putting replaceCurrentItemWithPlayerItem at the end of the scroll (delegate func scrollViewDidEndDecelerating(scrollView: UIScrollView) ). Now scrolling smoothly, yep!

Therefore, my advice is obvious: Using tools to analyze what exactly the user interface thread occupies

+1
source

All Articles