【问题标题】:View video in Landscape mode以横向模式观看视频
【发布时间】:2010-11-03 08:49:47
【问题描述】:

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

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

- (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:

发生了什么事?

【问题讨论】:

    标签: xcode ios ios4 landscape


    【解决方案1】:

    也有这个问题, 我在 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];
    

    重置视图。

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

    【讨论】:

      【解决方案2】:

      使用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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-23
        • 2014-11-21
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多