Allocating AVPlayer causes frames to drop - ios

I have a UICollectionViewController that autoplays video in its cells (similar to the Vine App). I'm finding that frames drop when scrolling, and I've isolated it to allocating the AVPlayer object in each cell. It doesn't matter if it's done on a background thread or on the main one. Are there any ways of working around this?

Related

Blank avplayer layer and no sound

After several (depends on device - 2-3 transitions on first SE, more than 20 transitions on 11) transitions between two vc in navigation stack (from custom camera vc to custom video player vc) I get blank player layer without any playing video or sound. I checked up memory graph and don't found any leaks. play(), seek() and other methods don't works. I tried to change player layer frame directly in this case but it has right values. Unfortunately there is unpredictable trouble inside custom player. Any ideas?

Is there a way to optimize multiple gifs playing at once in a collection view?

I'm building an iMessage extension where a lot of gifs are playing at once since the emoji that can be sent is animated. I'm currently using FLAnimatedImage to display gif images. All my gifs are being displayed in a collection view, and when the user scrolls it causes lots of lag.
Are there any other methods to display lots of gifs at once without causing lag?
I've tried using SwiftGIF, GIFSwift, and built in UIImage animation, but FLAnimatedImage causes the least amount of lag on scroll, but still very noticeable on slower devices.

Build up of CoreMedia.Player.Async threads when using AVPlayer in CollectionView

I am trying to do the stack overflow claimed impossible task of achieving ~60fps scroll performance in a collection view which contains some videos here and there (auto-playing). I have spent months on this and attempted every solution presented online and have gotten very very close. Creating the asset on a background thread and adding to the view is fine and having it auto play is all great, I can do that without any hiccups in performance.
The ONLY issue I have is that over time I build up a lot of instances of the player (even tho I am reusing the player and playerLayer in my cells...
Now this is fine for a while until the user has seen many videos and then eventually the videos just stop playing, obviously as there are so many instances active. To stop this build-up I need to remove the playerLayer from the superview every time, or replace the media item (which we all know runs on main thread and makes this task impossible).
SO, my question is, is there ANY OTHER WAY to kill off these old threads? If so, I will have finally found a solution to smooth scrolling with AVPlayer and I can return the favor to the community and post all my code!

Change the view that a video is playing on

In my app the user can play a video and leave the screen and it will continue to play in the background (just the audio). They can then return to continue to watch the video. This means that the view the video is in is destroyed and then recreated at a later point. Whenever the view is recreated and the player is set on it's AVPlayerLayer there is a noticeable lag in the video and more importantly the audio.
Does anyone know how to eliminate this lag?
The key to making this work without any lag / delay in the audio or video is to store the view with the AVPlayerLayer outside of the view. When reloading the controller, instead of creating a new view and assigning its player to the same player, simply attach the old view to the new view controller's view.
The view stays in memory as long as the video is still playing, that way a new AVPlayerLayer is not created and assigned to. It is the reassigning that causes the lag.

AVplayer full removal video out of memory

I know there have been a few hints on Stackoverflow about this issue but I haven't found a very satisfying answer to my problem.
I want to completely remove a video playing with AVPlayer in an AVPlayerLayer class from memory. I have read your are actually not allowed to call dealloc of the class containing the AVPlayerLayer. But even when I do so, the video remains in memory (cfr the sound does not stop playing).
Some people hint that you should pause the player, or just load in a new video, but I want it gone...
I need this because when rotating the device, I want the video kicked out of memory and a new video loaded in for the new orientation. I need to destroy the video completely because the parent view it is in also needs to be destroyed.
This is the structure I have
UIView
-> UIScrollView
-> UIviewWithPlayer
->AVPlayerLayer
In UIscrollView I call release and removefromsuperview on UIViewWithPlayer. In UIviewWithPlayer I call release and removefromsuperview on AVPlayerLayer. But that does not seem to work.
Thank you very much in advance for your help.

Resources