MPMoviePlayerController fullscreen quirk in iPad

后端 未结 7 1652
無奈伤痛
無奈伤痛 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-24 00:08

    Found it.

    Had the same problem - here is what I did. I would suggest adding the code to your project one by one to see exactly how it works.

    First - I put things is portrait mode.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    

    Then I shoved the movie down onto the status bar. Note - this assumes that the video has a 4x3 aspect ratio

    theVideo = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath : path]];
    float aspectRatio = (3.0f/4.0f);
    float theMovieHeight = [self view].bounds.size.width * aspectRatio;
    [[theVideo view] setFrame:(CGRectMake(0, [self view].bounds.size.height - theMovieHeight, [self view].bounds.size.width, theMovieHeight ))]; 
    

    Then, in the place where the application starts up (in my project, it is in the didFinishLaunchingWithOptions function) - anyway, you just need access to the window object.

    float aspectRatio = (3.0f/4.0f);
    float theMovieHeight = self.window.bounds.size.width * aspectRatio;
    float theSpaceAboveTheMovie = self.window.bounds.size.height - theMovieHeight;
    float whereTheMovieShouldBeCentered = (self.window.bounds.size.height - theMovieHeight) / 2;
    
    CGAffineTransform theTransform = CGAffineTransformMakeTranslation(0,0);
    
    theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
    theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
    theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
    
    [self.window setTransform:theTransform];
    

    Remember that affine transforms are done in reverse order. So if you want to see what each transform is doing (I suggest you should), comment out the first three

    Here you should see the movie and status bar centered on the page

    //  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
    //  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
    //  theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
        theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
    

    Then the first two

    Here you should see the movie and status bar rotated and no longer centered

    //  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
    //  theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
        theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
        theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
    

    Here you should see it rotated and centered

    //  theTransform = CGAffineTransformScale(theTransform, 1.0f/aspectRatio, 1.0f/aspectRatio);
        theTransform = CGAffineTransformTranslate(theTransform, -whereTheMovieShouldBeCentered, 0);
        theTransform = CGAffineTransformRotate(theTransform, M_PI / 2);
        theTransform = CGAffineTransformTranslate(theTransform, 0, -theSpaceAboveTheMovie);
    

    And with them all, it is rotated and fullscreen

    You can download my sample code here.

    0 讨论(0)
提交回复
热议问题