iOS - 仅允许视频横屏模式

3
我在我的应用程序中有许多视频。有些使用MPMoviePlayerController,其他的则在UIWebView中使用YouTube。我希望我的应用程序完全是竖屏。然而,当有视频时,我想给用户翻转到横屏的选项(不是强制性的,而是可选的)。
我一直在网上搜索答案,但我还没有找到任何东西。
感谢您的帮助!

如果您的应用程序是横向的,则只需执行以下操作:[mp shouldAutorotateToInterfaceOrientation:NO]; - Jatin
请看这里的答案:https://dev59.com/f2445IYBdhLWcg3wH2rH#14802980 - AMayes
你的应用程序中是否使用了rootViewController? - Madhu
5个回答

7

我曾经遇到过同样的问题,通过在我的应用程序委托中添加以下内容解决了它,基本上只允许MPMoviePlayerViewController的子类横向方向:

#import <MediaPlayer/MediaPlayer.h>

@implementation UIViewController (orientationFix)

-(NSUInteger) supportedInterfaceOrientations
{
    if ([[self class] isSubclassOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([[self class] isSubclassOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
    }
    return UIInterfaceOrientationPortrait;
}

@end

@implementation MyAppDelegate
.
.
.
@end

在我的情况下,我在普通视图控制器中使用YTPlayerView,因此这种方法不起作用,因为它将允许整个视图控制器处于横向状态,而不仅仅是在视频播放时。 - Van Du Tran
@VanDuTran 你好!请问你找到解决方案了吗?我也在使用YTPlayerView。另外,YTPlayer在真实设备上的声音是否正常工作? - Ne AS

0

在AppDelegate中添加以下代码:

-(BOOL)shouldAutorotate { return NO; }

-(NSUInteger)supportedInterfaceOrientations
{
    //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}

0

在应用程序委托中添加此代码。

-(BOOL)shouldAutorotate
    {
        return NO; 
    }

-(NSUInteger)supportedInterfaceOrientations
{
    //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}

0
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.view.transform = CGAffineTransformConcat(self.moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[self.moviePlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:self.moviePlayer.view];
[self.moviePlayer play];

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接