MPMoviePlayerViewController在横向/所有方向

时间:2013-01-25 09:29:22

标签: ios objective-c mpmovieplayercontroller screen-orientation

我的问题非常简单,教程和答案并没有解决我的问题。

我有一个设置应用程序:

enter image description here

我想在所有viewControllers中仅支持纵向/倒置方向,除非我想通过以下方式播放视频:

  

MPMoviePlayerViewController

这是代码:

MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[Videos videoURL:video.hash]];
if (mp) {
    isVideoPlaying = YES;

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(videoFinishedPlaying:)
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:mp.moviePlayer];

    [self presentMoviePlayerViewControllerAnimated:mp];
    mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [mp.moviePlayer play];
    [mp release];
}

MPMoviePlayerViewController播放视频时,我希望支持所有方向 需要你的帮助。

2 个答案:

答案 0 :(得分:2)

您应该通过以下方式允许轮换:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
}

IOS 6:

- (NSUInteger)supportedInterfaceOrientations
{

    return UIInterfaceOrientationMaskAll;
}

将代码放在您调用播放器的.m文件中

答案 1 :(得分:0)

大家好我遇到同样的问题我解决了 -

您需要先在appdelegate中进行更改:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here
{
    return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}

注册全屏控制通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
                                             name:MPMoviePlayerWillEnterFullscreenNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
                                             name:MPMoviePlayerWillExitFullscreenNotification
                                           object:nil];

然后在播放器控制器中添加代码行:

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^
               {
                   self.allowRotation = YES;
               });
}



- (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification
{
self.allowRotation = NO;
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];

dispatch_async(dispatch_get_main_queue(), ^
               {

                   //Managing GUI in pause condition
                       if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused)
                   {
                       [self.moviePlayerController pause];
                       if (self.playButton.selected)
                           self.playButton.selected = NO;
                   }
                   self.view.transform = CGAffineTransformMakeRotation(0);
                   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
                   self.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
               });
}

此代码在iOS6和iOS7中经过测试正常运行。感谢