开发者

View video in Landscape mode

开发者 https://www.devze.com 2023-01-23 11:10 出处:网络
I have a video in my application, but it\'s in portrait. I want to display it in landscape mode, but I don\'t have any idea how to do that.

I have a video in my application, but it's in portrait. I want to display it in landscape mode, but I don't have any idea how to do that.

I used this code to make my video:

- (IBAction)playMovie:(id)sender
{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"BAZO" ofType:@"m4v"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];

    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    //Uncomment om beeld formaat aan te passen
    //moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
    [moviePlayerController play];
}

- (IBAction)playSecondMovie:(id)sender
{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"00 01. Welcome" ofType:@"mov"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];

    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    //Uncomment om beeld formaat aan te passen
    //moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
    [moviePlayerController play];
}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}

-(void) tableView: (UITableView*) tableView 
didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"BAZO" ofType:@"m4v"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];

    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    //Uncomment om beeld formaat aan te passen
    //moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
    [m开发者_开发问答oviePlayerController play];

    /*-(void)loadVideo:(NSString *)name ofType:(NSString *)type{
    NSURL *url=[NSURL fileURLWithPath:[mainBundle pathForResource:@"BAZO"  ofType:@"m4V"]]
    if (!mp) mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [mp play];
    }*/
}

Maybe somebody can give me a hint or a method so I can put this to landscape?

I used this code:

[MPMoviePlayerController setOrientation:UIDeviceOrientationLandscapeLeft animated:NO];

but it gives me the warning :

MPMoviePlayerController may not respond to -setOrientation:animated:

What's happening?


Had this problem as well, I was running it in the appDelegate file, which meant I had to force the window to change to landscape so if the original is like this:

-(void)playMovie {
    NSString *url = [[NSBundle mainBundle] 
                     pathForResource:@"intro" 
                     ofType:@"m4v"];

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(movieFinishedCallback:)
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    [player setFullscreen:YES animated:YES];
    [player setControlStyle:MPMovieControlStyleNone];
    [window addSubview:player.view];
    [window bringSubviewToFront:player.view];
    [player setFullscreen:YES animated:YES];
    [player setControlStyle:MPMovieControlStyleNone];

    [player play];
}

all that is needed is to add these lines:

[window setBounds:CGRectMake(  0, 0, 480, 320)];
[window setCenter:CGPointMake(160, 240];    
[window setTransform:CGAffineTransformMakeRotation(M_PI/ 2)];

before the [player play], and at the movie finish call, add

struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;   
window =[[UIWindow alloc] initWithFrame:rect];
[window makeKeyAndVisible];

to reset the view.

Not sure if it's the right way to do it, but it was the only thing worked for me.


Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style, etc. I referred this answer for the same.

MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];

and in the AppDelegate.m, declare the following function which will restrict the orientation to portrait in all cases and will only change when a video is playing

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        //NSLog(@"orientation should change");
        return UIInterfaceOrientationMaskPortrait;
    }
}
0

精彩评论

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