Ilya
Ilya

Reputation: 144

How to properly rotate MPMoviePlayerController?

I'm having some issues properly rotating an instance of MPMoviePlayerController. I want it to rotate based on iPad orientation. Normally, it works. Sometimes, however, something strange happens, and then it's screwed until I restart the program.

Here's my initialization code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Drew 320x240" ofType:@"mp4" inDirectory:nil];
myMPC = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];

[[myMPC view] setFrame: CGRectMake(0, 0, 597, 448)];
myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(-90));
myMPC.view.center = CGPointMake(364, (850 / 2) + 168);

Here's my orientation code:

    -(void)orientationDetected:(UIEvent *)event{
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        if (myMPC) {
            myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(90));
            myMPC.view.center = CGPointMake(405, (850 / 2));
        }
    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        if (myMPC) {
            myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(-90));
            myMPC.view.center = CGPointMake(364, (850 / 2) + 174);
        }
    }
}

And here are some screenshots. The first two are how they're supposed to be (one with the home button left, and the other with the home button right), the third one is the screw up. Here they are:

Landscape Home Button Left

Landscape Home Button Right

Screw Up

As you can see, the video of the boy moves out of position and leaves blackness where it should have been (although the black rectangle isn't exactly where it should have been anyway -- it should be more to the left in that example). Once this happens, all subsequent device rotations result in this (until the program is restarted).

This is not a simulator-specific issue, as this happens on the actual iPad. Again, sometimes it rotates fine. Is there something I'm missing/does anyone know why this is happening/does anyone know how to prevent this?

I appreciate the help.

Thank you, Ilya

EDIT: Initialization code added and pictures are now embedded.

Upvotes: 2

Views: 3171

Answers (2)

Alexander van Elsas
Alexander van Elsas

Reputation: 111

I think setting an anchor point around which the video will rotate might solve your issues.

Try to init as follows:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Drew 320x240" ofType:@"mp4" inDirectory:nil];
myMPC = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];

[[myMPC view] setFrame: CGRectMake(0, 0, 597, 448)];
[[myMPC view].layer setAnchorPoint:CGPointMake( 0.5, 0.5 )]; // center of your view

The video should now rotate around its center.

You can find more details in the Apple Docs here

Upvotes: 0

Faizan S.
Faizan S.

Reputation: 8644

I had similar issues when I used MPMoviePlayerController the first time. The orientation just didn't work for me.

What I found was: MPMoviePlayerViewController (It contains a MPMoviePlayerControlle)

Using this new class, I had no more issues with the orientation. I was also able to define my own orientations in the shouldAutorotateToInterfaceOrientation by subclassing it. (Felt like magic :P)

Bear in mind that the MPMoviePlayerViewController class is called by [self presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController)]

Managing the orientation the way you're doing it right now is very tedious and error prone.

It can be done much easier using the built in classes in the framework.

HTH

Upvotes: 1

Related Questions