在横向模式下查看视频

时间:2010-11-03 08:49:48

标签: xcode ios ios4 landscape

我的应用程序中有一个视频,但它是纵向的。我想以横向模式显示它,但我不知道该怎么做。

我用这段代码制作了我的视频:

- (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;
    [moviePlayerController 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];
    }*/
}

也许有人可以给我一个提示或方法,所以我可以把它放到风景中?

我使用了这段代码:

[MPMoviePlayerController setOrientation:UIDeviceOrientationLandscapeLeft animated:NO];

但它给了我警告:

  

MPMoviePlayerController可能无法回复-setOrientation:animated:

发生了什么事?

2 个答案:

答案 0 :(得分:2)

也有这个问题, 我在appDelegate文件中运行它,这意味着我必须强制窗口更改为横向,所以如果原始是这样的:

-(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];
}

所需要的只是添加以下几行:

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

[player play]之前,在电影结束通话时,添加

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

重置视图。

不确定这是否是正确的方法,但这是唯一有效的方法。

答案 1 :(得分:0)

使用MPMoviePlayerViewController代替MPMoviePlayerController。它将处理方向,控制风格等。我将this answer称为相同的。

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

并在AppDelegate.m中声明以下函数,该函数会在所有情况下将方向限制为纵向,并且只会在播放视频时更改

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