Question

I have a horizontal flow layout for my UICollectionView. Each cell takes up the entire screen and paging is on. Each cell also contains an MPMoviePlayerController and I'm able to playback video without issue. However, if I scroll left or right the currently displayed movie (which is paused at this point) disappears - the player view goes black. If I then trigger the playback the video appears again. I've tried added prepareToPlay in the scrollview delegate methods to somehow keep the video visibile, but without success.

Was it helpful?

Solution

You can use only one instance of MPMoviePlayerController at once. This is a limitation with MPMoviePlayerController.

In your case, when you start to scroll, atleast two MPMoviePlayerController should be initialized and trying to play a video. This is not possible with MPMoviePlayerController.

If you want to play more than one video at once in the same screen, you might want to consider using AVFoundation for this. And there are open source video players based on AVFoundation. Usine one of them will be easier than creating a AVFoundation player from scratch.

OTHER TIPS

When you scroll the collection view the cells that go out of sight are re-used by the collection view "reusable pools" mechanism, so the existing movie player is removed and its status is reset. When the cell view is visible again then a view is taken from the pool (or created a new one if the pool is empty) and you need to reconfigure it. What can you do in such situation:

  • simple solution: give every single collection view cell a unique identifier (e.g. using the cell index path coordinates), so it is not recycled. Draw back of this approach: if you have many cells you could have memory problems as every single new cell is recreated from scratch; and the movie player controller takes non negligible memory
  • more complicated solution: take track of every single movie player status (playing time, video URL, ... you can do this by observing its parameters or listening to its notifications) on a dictionary, again indexed by the cell index path. When the cell goes out of sight the movie player is reset but when it is re-cycled you can reconfigure the movie player status to the right position. Obviously the operation is not immediate (the movie player takes time to start rendering the video) but you will not add extra memory pressure to your app.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top