I have an app with a tab bar controller at the root. The "home view" is a 3D rendered screen in OpenGL. There are certain 3D objects that you can click on that need to display a video. The video should be fullscreen and fade in and out.

To do this, I made the HomeViewController create a MPMoviePlayerViewController, assigned it a URL and then presented it from the tab bar controller. (I would have presented it from the HomeViewController but for some reason it didn't change its orientation properly--I'm sure it's related to all of the custom 3D stuff, and I didn't program it, so I just did a workaround by displaying it from a senior view.)

(Note that I am presenting the MPMoviePlayerViewController modally (not using the built-in presentModalMovieViewController or whatever) because Apple forces the transition to be the wacky screen-shift, and I wanted the dissolve.)

Now, that works fine. The modal window dissolves in, the video plays. You can play and pause it, fast forward, hit "Done" and the modal window goes away. Voila.

Now, here comes the totally weird bug: if you don't tap the video player and let the controls fade out (as they do after a second or two) the user cannot bring them back by tapping. It seems like the video controller stops responding to user input after that fade. Again, it works fine before they fade away. But after that point, I have to wait for the video to play all the way (at which point the modal window does, in fact, go away).

For reference, here is the relevant code to the modal video player:

-(void) startVideoWithURL:(NSURL *)videoURL {

    if (!self.outsideMoviePlayerViewController) {
        self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
    }

    if (videoURL) {

        [self stopAnimation];

        self.outsideMoviePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        self.outsideMoviePlayerViewController.moviePlayer.contentURL = videoURL;

        [[[AppController instance] getCustomTabBarViewController] presentModalViewController:self.outsideMoviePlayerViewController animated:YES];

        // Move observation of the dismiss from the MoviePlayerViewController to self.
        [[NSNotificationCenter defaultCenter] removeObserver:self.outsideMoviePlayerViewController
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:self.outsideMoviePlayerViewController.moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.outsideMoviePlayerViewController.moviePlayer];

    }

}

-(void) movieFinishedCallback:(NSNotification *)aNotification {

    // Summary: Restart 3D animation, unregister from notifications, kill the modal video.

    [self startAnimation];

    MPMoviePlayerController *moviePlayer = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];

    [[[AppController instance] getCustomTabBarViewController] dismissModalViewControllerAnimated:YES];

}

The only other reference I could find to an issue like this was some archived post on the Apple Support Communities, here:

https://discussions.apple.com/thread/2141156?start=0&tstart=0

In this thread, the issue poster figures it out himself, and states that the issue was resolved. Here's his explanation:

The problem occurs when CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE). After i changed to CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002, TRUE), the playback control can appears/disappear by tapping the screen.

Unfortunately, I'm not programming a game and nobody on my dev team calls CFRunLoopRunInMode anywhere in the code. The closest thing I found to this was in the animation code (in the same ViewController):

- (void)startAnimation
{
    if (!animating)
    {
        NSLog(@"startAnimation called");
        CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(drawFrame)];
        [aDisplayLink setFrameInterval:animationFrameInterval];
        [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        self.displayLink = aDisplayLink;

        animating = TRUE;
    }
}

If anyone has any insights on what could be causing this, I would appreciate it. I figured that, at the very least, even if I figure it out myself tonight, this problem could go up on Stack Overflow and be archived for the sake of posterity.

Cheers.

有帮助吗?

解决方案

I figured out what was causing this problem.

I noticed that the first video did play, while successive ones did not. I moved this code:

if (!self.outsideMoviePlayerViewController) {
        self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
}

This way the creation of the outsideMoviePlayerViewController was inside the next block, like so:

if (videoURL) {

        self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];

        [self stopAnimation];

        self.outsideMoviePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        self.outsideMoviePlayerViewController.moviePlayer.contentURL = videoURL;

Now a new controller is created instead of recycling the controller each time I played a video. The bug went away. I'm not 100% sure why this happened, because there are a variety of things that occur when you display a modal view controller.

The bottom line is probably that I should have done this in the first place as part of the lazy loading paradigm instead of trying to keep the controller in memory.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top