开发者

MPMoviePlayerController controls when opened in a UIModalPresentationFormSheet style modal and the user makes the video fullscreen

开发者 https://www.devze.com 2023-04-12 06:44 出处:网络
In my iPad app, I have a UIButton that is calling an IBAction to call a view controller as a modal to show a video in. 开发者_如何转开发 I wanted the modal to appear as 720x405, and that part seems to

In my iPad app, I have a UIButton that is calling an IBAction to call a view controller as a modal to show a video in. 开发者_如何转开发 I wanted the modal to appear as 720x405, and that part seems to work out okay. Here is the IBAction code the button is executing:

    -(IBAction)videoPlayerTest:(id)sender {
        VideoModalViewController *vc = [[VideoModalViewController alloc] initWithNibName: @"VideoModalViewController" bundle: nil];
        vc.fileName = @"testvideo.m4v";
        vc.modalPresentationStyle = UIModalPresentationFormSheet;
        [self presentModalViewController:vc animated: YES];
        vc.view.superview.frame = CGRectMake(0, 0, 720, 405); 
        vc.view.superview.center = self.view.center;
        [vc release];
    }

The modal comes up where I want it, and the controls respond on the MPMoviePlayerController; the jog bar, pause, play, etc. but if the user taps on the fullscreen button, the video does go fullscreen alright, but after that the MPMoviePlayerController won't respond to any subsequent taps on the player controls. If I remove the modalPresentationStyle line it will work, but the modal appears on a fullscreen view instead of the 720x405 modal like I want. I've added Observers to try resizing the frame and recenter it when the user makes the movie controller fullscreen and back to windowed, but it didn't appear to help at all. Here is that code.

    - (void)willEnterFullscreen:(NSNotification*)notification {
        NSLog(@"willEnterFullscreen");
        [self setModalPresentationStyle:UIModalPresentationFullScreen];
        self.view.frame = CGRectMake(0, 0, 1024, 768);
        self.view.center = self.view.center;
    }

    - (void)willExitFullscreen:(NSNotification*)notification {
        NSLog(@"willExitFullscreen");
        [self setModalPresentationStyle:UIModalPresentationFormSheet];
        self.view.frame = CGRectMake(0, 0, 720, 405);
        self.view.center = self.view.center;
    }

    - (void)playMovie {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

        NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
        NSURL *videoURL = [NSURL fileURLWithPath:videoString];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
        [self.view addSubview:moviePlayer.view];
        moviePlayer.view.frame = CGRectMake(0, 0, 720, 405);
        moviePlayer.view.backgroundColor = [UIColor grayColor];
        [moviePlayer prepareToPlay];
        [moviePlayer play];
    }

This is my first post-- hope I did it right and provided enough information about the problem I'm having.


I've solved my problem. I was unaware of MPMoviePlayerViewController and I created that and used that as my modal instead. It works great.

    -(void)playVideo:(NSString *)fileName {
        NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
        NSURL *videoURL = [NSURL fileURLWithPath:videoString];
        mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
        [self presentModalViewController:mpViewController animated:NO];
        [[mpViewController moviePlayer] play];
    }

    -(IBAction)videoPlayerTest:(id)sender {
        [self playVideo:@"testvideo.m4v"];
    }

Thought I'd post what I came up with just in case somebody else encounters the same

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号