解决iOS工程 默认竖屏 MPMoviePlayerController 全屏不能横屏的情况

In AppDelegate.h:
@property(nonatomic)BOOL allowRotation;

in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    RootViewController * root = [[RootViewController alloc] init];
    self.window.rootViewController = root;

//add two Notification

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

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

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

- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
    self.allowRotation = NO;
}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {

        return UIInterfaceOrientationMaskLandscapeRight ;
    }
    return UIInterfaceOrientationMaskPortrait;
}

参考:http://stackoverflow.com/questions/20285356/mpmovieplayercontroller-can-rotate-in-full-screen-while-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以提供一个Objective-C编写的播放视频的示例代码,它可以帮助你快速实现播放视频的功能: // 初始化播放器 AVPlayer *player = [[AVPlayer alloc] initWithURL:url]; // 设置播放器控制器 AVPlayerViewController *playerController = [[AVPlayerViewController alloc] init]; playerController.player = player; // 设置播放器的位置 playerController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); // 添加播放器 [self.view addSubview:playerController.view]; // 开始播放 [player play]; ### 回答2: Objective-C是一种广泛用于iOS和macOS应用开发的编程语言。下面是一个使用Objective-C编写的简单播放视频的案例代码: 首先,需要导入AVFoundation框架和MediaPlayer框架,以便使用相关的类和方法。 ```objective-c #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> ``` 在视图控制器或适当的地方创建一个媒体播放器对象和一个播放视频的方法: ```objective-c - (void)playVideo { // 视频路径 NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]]; // 创建媒体播放器 AVPlayer *player = [AVPlayer playerWithURL:videoURL]; // 创建视频显示层 AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; // 播放视频 [player play]; } ``` 在上述代码中,video.mp4是存储在应用程序资源文件中的视频文件,可以根据需求更改视频文件的路径和名称。 在适当的时候调用`playVideo`方法,例如在视图加载完成时或在按钮点击事件中。 请注意,以上仅为一个简单的播放视频的案例,实际项目中可能需要更多的功能和控制,例如播放暂停、进度控制、全屏播放等功能,您可以根据实际需求进行扩展和修改。 ### 回答3: Objective-C是一种用于iOS和Mac开发的编程语言,它可以通过使用内置的媒体框架来实现视频播放。下面是一个用Objective-C编写的简单示例来演示如何播放视频。 首先,我们需要导入MediaPlayer框架来实现视频播放。在Objective-C中,可以使用以下代码导入: #import <MediaPlayer/MediaPlayer.h> 然后,在视图控制器中创建一个MPMoviePlayerController的实例来管理视频播放。这可以通过以下代码完成: MPMoviePlayerController *playerController = [[MPMoviePlayerController alloc] init]; 然后,我们需要设置视频资源的URL。这可以是一个本地文件URL或来自网络的URL。例如,如果视频文件名为“video.mp4”,并且位于应用程序的主包中,则可以使用以下代码设置URL: NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"]; [playerController setContentURL:videoURL]; 接下来,我们需要将播放器视图添加到视图层次结构中,以便将视频显示在屏幕上。这可以通过以下代码完成: [playerController.view setFrame:self.view.bounds]; [self.view addSubview:playerController.view]; 最后,我们可以调用play方法来开始播放视频: [playerController play]; 这样,我们就完成了使用Objective-C编写一个简单的视频播放案例。当用户运行该应用程序时,它将显示播放器界面并自动开始播放视频。用户可以通过播放器界面上的控件来控制视频的暂停、调整音量等操作。 需要注意的是,这只是一个简单的示例,实际的视频播放案例可能需要更复杂的功能和逻辑。但是,上述代码足以演示Objective-C中实现视频播放的基本概念。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值