iOS Objective-C Xcode - 如何开始仅在Landscape中播放视频

时间:2016-11-15 09:44:41

标签: ios objective-c iphone xcode

我正在制作一个应用程序来播放360个视频 - 一切都很棒!

但是,我希望视频只有在移动设备面向风景时才会开始播放!如果是纵向,它应显示一条消息“请将您的手机置于风景中”,如果是,则开始播放视频。 ..

有人知道如何实现这个目标吗?

这将是惊人的:))

谢谢!

编辑:

我的Viewcontroller.m

//
//  ViewController.m
//  video360test
//
//  Created by linyize on 16/6/20.
//  Copyright © 2016年 islate. All rights reserved.
//

#import "ViewController.h"

#import "Video360ViewController.h"

#import "CardboardViewController.h"
#import "CardboardSDK.h"

@implementation ViewController

- (BOOL)prefersStatusBarHidden {
return YES;
}

- (BOOL)shouldAutorotate
{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)playURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://7b1gcw.com1.z0.glb.clouddn.com/demo1.mp4"];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil ];

}
}

- (IBAction)playFileffpvr:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];

Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];


[videoController VRMode:true];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil];
}
}

- (IBAction)playFileff360:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil];
}
}

- (IBAction)playFile2:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"boa" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil];
}
}

- (IBAction)playFileffp:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"boa" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil];
}
}

@end

@implementation LandscapeNavController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationBarHidden=YES;
}

-(BOOL)shouldAutorotate{

return YES;
}

- (NSUInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskLandscape;
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

编辑2:

按钮现在正确显示警报:

- (IBAction)playFileffpvr:(id)sender

{
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

if (orientation == UIDeviceOrientationPortrait)
    [self doSomething];
else
    [self doSomethingElse];



Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

[videoController VRMode:true];

if (![[self presentedViewController] isBeingDismissed]) {
    [self presentViewController:videoController animated:YES completion:nil];
}
}

并显示警告:

-(void)doSomething
{

//Show Alert

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Simple" message:@"Turn your device to Landscape." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    NSLog(@"Cancel");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    NSLog(@"OK");
}];

[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated: YES completion: nil];
}

-(void)doSomethingElse
{
//Function Body
//play the file
}

2 个答案:

答案 0 :(得分:0)

是的,您需要做的就是创建一个UINavigationController子类说LandscapeNavController并将代码放在下面

<强> LandscapeNavController.h

#import <UIKit/UIKit.h>

@interface LandscapeNavController : UINavigationController

@end

<强> LandscapeNavController.m

@implementation LandscapeNavController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBarHidden=YES;
}

-(BOOL)shouldAutorotate{

    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskLandscape;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

现在将视频控制器呈现为模式,如下所述

VideoController *controller=<INITIALIZE>
LandscapeNavController *nav=[[LandscapeNavController alloc] initWithRootViewController:controller];
[self presentViewController:nav animated:YES completion:nil];

并确保您的旋转设置

enter image description here

另外在 AppDelegate.h 定义

@property(assign)BOOL shouldRotate;

上述属性应设置为 YES ,然后才能解除< VideoController NO 强>的VideoController

并在 AppDelegate.m

中添加以下代码
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    if (shouldRotate)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
}

注意 - 如果您遇到问题ping,上面的代码现在没有测试过。

干杯。

答案 1 :(得分:0)

UIAlertController的属性声明为:

@property (strong, nonatomic)UIAlertController *alertController;

您可以使用以下代码:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}

-(void)deviceOrientationDidChange
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationPortrait)
         [self doSomething];
        else
             [self doSomethingElse];
    }
-(void)doSomething
{

//Show Alert

self.alertController = [UIAlertController alertControllerWithTitle:@"Simple" message:@"Turn your device to Landscape." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        NSLog(@"Cancel");
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSLog(@"OK");
    }];

    [self.alertController addAction:cancelAction];
    [self.alertController addAction:okAction];
    [self presentViewController:alertController animated: YES completion: nil];
}
-(void)doSomethingElse
{
//Hide the AlertViewController
[self.alertController removeFromSuperview]; 
//Code to handle the playing of the file
}
-(BOOL)shouldAutorotate{

    return YES;
}

快乐编码。希望它有所帮助。