MPMoviePlayerController fullscreen quirk in iPad

后端 未结 7 1807
無奈伤痛
無奈伤痛 2020-12-23 23:22

I want to show a MPMoviePlayerController in a view controller and let the user toggle full screen with the default controls, like the YouTube app. I\'m using the following c

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 23:47

    Had the same problem, just spent half a day sorting it out. With the iPad in portrait orientation, whenever I started a video using the sample code (or any I could find on the net) the video and control bar were formatted for portrait, and hence all over the place on the screen.

    Anyway, the following works for me.

      /* Call the code like below:
            int iLandscape;
            if( newOrientation==UIInterfaceOrientationLandscapeLeft || newOrientation==UIInterfaceOrientationLandscapeRight )
                 iLandscape=1;
    
            [self PlayVideo:iLandscape fullscreen:1]
        */
            //////////////////////////////////////////////////////////////////////////
            - (void)PlayVideo:(int)iLandscape fullscreen:(int)iFullScreen 
            {
                NSString *url = [[NSBundle mainBundle] pathForResource:@"myvideofile" ofType:@"m4v"];
    
            if( iFullScreen==0 )
            {
                MPMoviePlayerController *player2 = 
                    [[MPMoviePlayerController alloc] 
                        initWithContentURL:[NSURL fileURLWithPath:url]];
    
                [[NSNotificationCenter defaultCenter] 
                    addObserver:self
                       selector:@selector(movieFinishedCallback:)
                           name:MPMoviePlayerPlaybackDidFinishNotification
                         object:player2];
    
                //---play partial screen---
                player2.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
                [self addSubview:player2.view];
                //---play movie---
                [player2 play];
            }   
            else
            {
                MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] 
                    initWithContentURL:[NSURL fileURLWithPath:url]];
    
                [[NSNotificationCenter defaultCenter] 
                    addObserver:self
                       selector:@selector(movieFinishedCallback:)
                           name:MPMoviePlayerPlaybackDidFinishNotification
                         object:[playerViewController moviePlayer]];
    
                if( iLandscape )
                {
                    playerViewController.view.frame = CGRectMake(0, 0, m_iScreenWidth, m_iScreenHeight);
                }
                [self addSubview:playerViewController.view];
                //play movie
                MPMoviePlayerController *player = [playerViewController moviePlayer];
                player.scalingMode=MPMovieScalingModeAspectFit;
                [player play];
            }
        }
    
    
        //////////////////////////////////////////////////////////////////////////
        - (void) movieFinishedCallback:(NSNotification*) aNotification 
        {
            MPMoviePlayerController *player = [aNotification object];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    
            [player autorelease];    
            [player.view removeFromSuperview];
        }
    

提交回复
热议问题