ios - 设置 MPMoviePlayerController 的方向

标签 ios iphone mpmovieplayercontroller uiinterfaceorientation

我的整个应用程序仅处于纵向模式,我正在我的应用程序中播放 youtube 视频。对于你管我使用 UIWebview。当用户单击 UIWebview 中的播放按钮时,它会自动启动 MPMoviePlayerController。所以我没有声明任何 MPMoviePlayerController 对象。所以我希望 MPMoviePlayerController 支持纵向和横向。所以请提出建议。

最佳答案

如果您使用 NavigationController,您可以将其子类化并执行以下操作:

#import "MainNavigationController.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation MainNavigationController

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }

}
@end

然后您应该将您的应用设置为支持所有方向,并且此代码将仅在播放您的电影“MPMoviePlayerController”时允许方向更改。

当调用你的movie时,你应该发送一个通知,所以如果用户以portrait以外的任何方向关闭它,它会切换回portrait .

像这样:

- (IBAction)playButton:(id)sender {

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

    NSURL *url = [NSURL URLWithString:videoUrl];

    self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    self.player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

    [self presentMoviePlayerViewControllerAnimated:self.player]; 
}

-(void)moviePlaybackDidFinish
{
    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                forKey:@"orientation"];
}

这应该可以为您完成,让我知道进展如何。

关于ios - 设置 MPMoviePlayerController 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26257615/

相关文章:

iphone - 无法使用 MPMoviePlayerController 进行搜索

ios - 使用 MPMoviePlayerController 时的内存泄漏

android - 为每个数据条目在一行上显示带有按钮的数据

ios - 如何在 Swift 中对 simd_float4x4 元素数组进行编码(将 simd_float4x4 转换为数据)?

iPhone LINE-IN 录音 可以吗?

iphone - 从应用程序编辑 iPhone 音乐 ID3 标签

ios - 在 UIPickerView 中显示来自多个组件的文本

iOS 企业版 : What should I change or keep the same so an updated app installs over the older version?

iphone - "Gallery"app在IOS中分享 View

iphone - MPMoviePlayerController 不工作,也没有通知