【问题标题】:iOS: How to change orientation of MPMoviePlayerViewController on iPad?iOS:如何在 iPad 上更改 MPMoviePlayerViewController 的方向?
【发布时间】:2012-09-24 10:30:57
【问题描述】:

我正在开发一个以非常短的介绍视频开头的通用应用程序。所以我只需添加一个 MPMoviePlayerViewController 并以模态方式呈现它。即使我以横向模式播放视频,在 iPhone 上一切正常。但在 iPad 上,无论设备当前具有何种界面方向,视频始终以纵向模式播放。我搜索了几个小时并尝试了很多不同的东西,比如 CGAffineTransformation 或添加新视图,但似乎没有任何效果。这是我使用的代码:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];  

    self.startupController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    [self.startupController.moviePlayer setControlStyle:MPMovieControlStyleNone];
    [self.startupController.moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
    [self.startupController.moviePlayer setFullscreen:YES];        
    [self presentModalViewController:self.startupController animated:NO];

您知道如何解决此问题吗?在我看来,这应该是 Apple 提供的一个简单功能,但有时最简单的事情是我最需要担心的......

【问题讨论】:

    标签: ios ipad movieplayer


    【解决方案1】:

    这就是我的做法。 首先,收听这个通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    

    然后实现这个:

    - (void)detectOrientation {
        NSLog(@"handling orientation");
        [self setMoviePlayerViewOrientation:[[UIDevice currentDevice] orientation]];
    }
    
    
    - (void)setMoviePlayerViewOrientation:(UIDeviceOrientation)orientation {
    if (lwvc != nil)
        return;
    if (moviePlayerController.playbackState == MPMoviePlaybackStateStopped) return;
    //Rotate the view!
    CGFloat degree = 0;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.1];
    switch (orientation) {
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            degree = 0;
            moviePlayerController.view.bounds = CGRectMake(0, 0, 320, height);
            break;
        case UIDeviceOrientationLandscapeLeft:
            degree = 90;
            moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
            break;
        case UIDeviceOrientationLandscapeRight:
            degree = -90;
            moviePlayerController.view.bounds = CGRectMake(0, 0, height, 320);
            break;
        default:
            break;
    }
    lastOrientation = orientation;
    CGAffineTransform cgCTM = CGAffineTransformMakeRotation((degree) * M_PI / 180);
    moviePlayerController.view.transform = cgCTM;
    [UIView commitAnimations];
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation];
    }
    

    所以诀窍就是使用转换

    【讨论】:

    • 感谢您的快速回答。但这真的可以解决我的问题吗?我认为 UIDeviceOrientationDidChangeNotification 永远不会被调用,因为设备方向没有改变。该应用程序只是以不同的方向启动。你明白我的问题吗?
    • 我终于成功了。非常感谢。我不需要观察设备方向。我只是在调用 MPMoviePlayerViewController 并使用你的函数后切换它。干得好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多