iOS:电影播放器​​视图不会旋转 [英] iOS: Movie player view doesn't rotate

查看:208
本文介绍了iOS:电影播放器​​视图不会旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明确地旋转我的电影播放器​​视图(不使用Autorotation delegate)。我为此编写了以下代码,并在其中添加了注释。电影播放器​​的父视图确实旋转但不是电影播放器​​视图本身。有人可以在这帮助我,我做错了吗?谢谢。

I want to rotate my movie player view explicitly (not using Autorotation delegate). I wrote following code for that and also put comments in it. The parent view of movie player does rotate but not movie player view itself. Could someone please assist me here what I am doing wrong? Thanks.

#import "CustomMoviePlayer.h"
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(x) ((x) * 180.0/M_PI)

@implementation CustomMoviePlayer

@synthesize moviePlayer, myParentViewController;

-(void)playMovie:(NSString*)filePath onViewController:(UIViewController*)controller
{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotateMoviePlayer) name: UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];   

    myParentViewController = controller;

    NSURL *url = [NSURL fileURLWithPath:filePath];

    moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:url];
    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;

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

    [controller.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];
}

- (void)moviePlaybackStateChanged:(NSNotification *)notification
{
    NSLog(@"State changed... %d", [moviePlayer playbackState]);
}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    NSLog(@"Finished video...");

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];

    [myParentViewController.view setTransform:CGAffineTransformIdentity];
    [moviePlayer.view removeFromSuperview];
    [moviePlayer release];
}

-(void)rotateMoviePlayer
{
    NSLog(@"Rotate movie player");

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    if (orientation != UIDeviceOrientationUnknown) {

        CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadian(0));

        switch (orientation) {
            case UIDeviceOrientationLandscapeLeft:
                transform = CGAffineTransformMakeRotation(M_PI / 2); 
                break;
            case UIDeviceOrientationLandscapeRight:
                transform = CGAffineTransformMakeRotation(-M_PI / 2);
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                transform = CGAffineTransformMakeRotation(M_PI);
                break;  
            default:
                break;
        }
        // This view does rotate and I can see it rotating when there is no moviePlayer on top of it!
        [myParentViewController.view setTransform:CGAffineTransformIdentity];
        [myParentViewController.view setTransform:transform];

        // It doesn't mater whether I put following two lines or not...Movie player view doesn't rotate!
        [moviePlayer.view setTransform:CGAffineTransformIdentity];
        [moviePlayer.view setTransform:transform];
    }    
}

- (void)dealloc {
    [moviePlayer release];
    [super dealloc];
}

@end


推荐答案

您在全屏模式下使用 MPMoviePlayerController ,因此应用于其视图(和/或其超视图)的旋转/无效。

You are using MPMoviePlayerController in fullscreen mode, hence the rotation/s applied to its view (and/or its superview) are not being effective.

使用全屏模式时, MPMoviePlayerController 实际上是通过将其渲染层直接添加到UIWindow来使用不同的方法 - 也就是说,它确实没有使用它的视图属性。

When using fullscreen mode, MPMoviePlayerController is actually using a different approach by adding its rendering layer directly to a UIWindow - that is, it does effectively not use its view property.

获取当前的 UIWindow 当电影以全屏模式播放时,您可以使用以下片段;

For getting the current UIWindow when a movie is playing in fullscreen, you may use the following snippet;

UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}

进入该窗口后,直接将旋转/直接应用于此窗口。

Once you got that window, apply your rotation/s directly to it.

然而,这将导致许多难以克服的问题(相信我,去过那里,得到了T恤的集合)。作为替代方式,我强烈建议你使用假全屏模式。

This however will result in many possible issues that are hard to overcome (trust me, been there, got a collection of Tshirts). As an alternative way, I would strongly suggest you to use a fake-fullscreen mode.

而不是像你那样初始化播放器

Instead of initializing the player like you did

[moviePlayer setFullscreen:YES animated:YES];

为什么不简单地将其视图的帧大小初始化为整个屏幕 - 或者像这样的超级视图的界限

Why not simply initializing its view's frame size to the entire screen - or the bounds of its superview like this

moviePlayer.view.frame = myParentViewController.view.bounds;

然后,要获得全屏界面,请使用以下控件样式:

Then, for getting the fullscreen interface, use the following control style:

moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

这样,你得到的播放将遵循在superview上完成的任何转换,你将免于副作用。

That way, your resulting playback will adhere to any transformation done on the superview and you will be free of side effects.

这篇关于iOS:电影播放器​​视图不会旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆