iphone - force MPMoviePlayerController to play video in landscape mode

后端 未结 5 1288
攒了一身酷
攒了一身酷 2020-12-17 05:28

I have an app that is portrait mode only, but when the user plays a video I want it to play in fullscreen landscape mode (the video player doesn\'t look good in portrait mod

5条回答
  •  粉色の甜心
    2020-12-17 06:15

    I had the same situation, but I am using iOS 6 and a NavController based project. What makes that interesting is the ViewController that is hosting the MPMoviePlayerController I don't want rotated, but I wanted the video within it to be rotated.

    ViewController with rotated MPMoviePlayerController
    I just manually rotate the MPMoviePlayerController as needed based on the devices orientation. _videoView is a MPMoviePlayerController property of the ViewController. For the example, I just hard coded the desired width and height to a 16x9 aspect ratio as I intend this video to be uploaded to youtube.

    - (void)updateVideoRotationForCurrentRotationWithAnimation:(bool)animation
    {
        CGSize containerSize  = _videoView.frame.size;   // Container NOT rotated!
        float  videoWidth     = 384;                     // Keep 16x9 aspect ratio
        float  videoHeight    = 216;
    
        if( animation )
        {
            [UIView beginAnimations:@"swing" context:nil];
            [UIView setAnimationDuration:0.25];
        }
    
        switch( self.interfaceOrientation )
        {
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:
                m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(90));
    
                // This video player is rotated so flip width and height, but the container view
                // isn't rotated!
                [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoHeight)/2,
                                                        (containerSize.height-videoWidth)/2,
                                                        videoHeight,
                                                        videoWidth)];
                break;
    
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
                m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(0));
    
                // This video player isn't rotated
                [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoWidth)/2,
                                                        (containerSize.height-videoHeight)/2,
                                                        videoWidth,
                                                        videoHeight)];
                break;
        }
    
        if( animation )
            [UIView commitAnimations];
    
    }
    
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        [self updateVideoRotationForCurrentRotationWithAnimation:YES];
    }
    

    I also call updateVideoRotationForCurrentRotationWithAnimation after view did appear so it has the correct initial orientation.

提交回复
热议问题