Search code examples
iosvideompmovieplayercontroller

MPMoviePlayerViewController Black Screen When Navigating To/From Another Section


I have a tabbed application. Each tab loads a table view controller inside a navigation controller to maintain the upper navigation bar.

The table view controller presents a list of videos. Upon clicking a table cell, the user is presented with a MPMoviePlayerViewController which loads the requested video using the following code:

VideoViewController * playerController = [[VideoViewController alloc] initWithContentURL:url];
[self.navigationController pushViewController:playerController animated:YES];
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer play];

UIBarButtonItem *mailButton = [[UIBarButtonItem alloc] initWithTitle:@"Email Link" style:UIBarButtonItemStylePlain target:self action:@selector(openMailBasket:)];
playerController.navigationItem.rightBarButtonItem = mailButton;

Note: VideoViewController is a subclass of MPMoviePlayerViewController.

This works great. However, if I click a tab at the bottom to navigate to another section, then go back to the Videos section, it misbehaves.

In IOS 5, the video starts back at the beginning. This is fine.

In IOS 6, I get a blank video screen that says "Loading..." and never loads.

In IOS 7, I am given a black video player with a total time of 0:00.

Its like the video has been 'unloaded' in IOS 6 and IOS 7. However, if I execute this in viewWillAppear:

NSString *vidURL = [self.moviePlayer.contentURL absoluteString];
NSLog(@"URL: %@",vidURL);

The URL is valid.

Note: I must use MPMoviePlayerViewController instead of MPMoviePlayerController because I need the 'Email Link' button to appear at the top.

Do I need to do anything special to get MPMoviePlayerViewController to maintain the video's state when I navigate elsewhere? Is ARC deallocating my video, and if so, why does it work in IOS 5, and what can I do about it?


Solution

  • Well, apparently typing it out on here led me to find the solution, so I'll lay it out.

    I got to thinking about why it was working on IOS 5 and not 6 or 7. It seemed to me that ARC was probably autoreleasing my video data, and I needed a way to tell IOS that I want to bring the movie back.

    All I needed to do was add:

    -(void)viewWillAppear:(BOOL)animated
    {
        [self.moviePlayer prepareToPlay];
    }
    

    to my VideoViewController class.