Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible to view an MPMoviePlayerController within a UICollectionView?
    text
    copied!<p>I'm trying to build a UICollectionView in which each cell holds an MPMoviePlayerController with a different video. I want the user to be able to play each video in place.</p> <p>I know that you can't play multiple instances of an MPMoviePlayerController. What's vague about Apple's documentation is whether or not you can instantiate and place them in views - the way I read it, you actually CAN do that. However, I couldn't get that to work so I finally gave up and tried a different technique.</p> <p>Instead, I tried using a select event to instantiate, place, and automatically play the video within the collection cell. As a control, I placed an additional view outside of the collection view, within the same UIViewController. When I ran it, the outside view loaded up just fine - the video instantiated and the first frame appeared. And all the cells in the collection view appeared correctly, as well. However, when I selected one of the collection cells, the video in the outside view disappeared entirely while the video in the collection view instantiated but failed to appear. I know it instantiated because I color-coded the background of each of the views and on selecting the cell, the background for the video appeared, but the actual video did not. Very confusing.</p> <p>So I'm wondering if you can even put a video within a collection view at all?</p> <p>Here's the code for the view controller:</p> <pre><code>#import "testViewController.h" #import &lt;MediaPlayer/MediaPlayer.h&gt; #import "tvCollectionCell.h" @interface testViewController () @property (strong) IBOutlet UIView *containerView; @property (strong) MPMoviePlayerController *videoPlayer; @end @implementation testViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewDidAppear:(BOOL)animated { self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"testvideo" ofType:@"mp4"]]]; CGRect videoFrame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height); [[self.videoPlayer view] setFrame:videoFrame]; self.videoPlayer.shouldAutoplay = FALSE; self.videoPlayer.repeatMode = TRUE; [self.videoPlayer prepareToPlay]; [self.videoPlayer view].backgroundColor = [UIColor orangeColor]; [self.containerView addSubview:[self.videoPlayer view]]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { tvCollectionCell *tvCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tvCell" forIndexPath:indexPath]; tvCell.backgroundColor = [UIColor purpleColor]; return tvCell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { tvCollectionCell *tvCell = (tvCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"testvideo" ofType:@"mp4"]]]; CGRect videoFrame = CGRectMake(0, 0, tvCell.frame.size.width, tvCell.frame.size.height); [[movie view] setFrame:videoFrame]; movie.shouldAutoplay = TRUE; movie.repeatMode = TRUE; [movie prepareToPlay]; [movie view].backgroundColor = [UIColor blueColor]; [tvCell addSubview:[movie view]]; } @end </code></pre> <p>Thanks for the help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload