Multiple AVPlayers

I recently built a video creation and sharing app, something like Instagram for video. This app has a feed of videos, up to one hundred AVPlayer’s in a single paged UIScrollView. A feed is a collection of AVPlayer’s in a single paged UIScrollView, where feeds can be stacked on top of one another using a UINavigationController. With only five or six videos loaded in each AVPlayer – there are no issues at all. However, when 10 or more AVPlayer’s have been allocated and are active across various feeds, performance starts to deteriorate quickly. Memory warnings pop up everywhere.

My first attempt was to ensure that only three AVPlayer’s would be allocated at any one time. Even this proved to be insufficient with various hiccups in performance. Finally, I realized that only one AVPlayer should be allocated at anyone time for optimal performance. Thus I created a singleton class especially for this AVPlayer.

The second issue to address is download of the videos and a clever and intuitive way to indicate to the user the download progress. I opted for a UIBlurEffect on top of the video still image. As the download progresses, the alpha attribute of the UIBlurEffect is reduced to 0. When download is complete, the AVPlayer is allocated and the video is loaded into the AVPlayer.

Lastly is the issue of downloading the videos themselves. With a WiFi connection, it is possible to download as many as 20 videos as once, but with an LTE connection – it is best to download one video at a time. I catered for the lowest common denominator and reasoned that only one video should be downloaded at a time – to be stored in the app’s documents folder. Download of video is initiated upon swipe to a new feed item.