2016-11-15 55 views
0

我正在构建一个应用程序来播放360视频 - 一切都很好!iOS Objective-C Xcode - 如何开始播放风景视频

但是,我希望视频仅在手机以横向为导向时才开始播放!如果在肖像中,它应该显示一条消息“请把你的手机放在风景中”,如果是的话 - 开始播放视频。 ..

任何人有什么想法如何实现这一目标?

这将是惊人的:)

谢谢!


编辑:

我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 
} 
+0

您是否在使用MPMoviePlayerController播放视频? –

+0

谢谢回复, 不,我使用https://github.com/islate/SlateVideo360 但是,如果你有什么用MPMovie工作,我也许能使其与一个工作? – tibewww

+0

如果您正在演示视频视图控制器,你可以强制它是唯一风景.... – Lefteris

回答

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 should旋转;

您驳回视频控制器前出示视频控制器NO之前,上述属性应设置为YES

而且在AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 

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

注意添加以下代码 - 上面的代码没有经过测试,现在,如果你面对的问题平。

干杯。

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; 
} 

编码愉快。希望能帮助到你。

+0

嗨易卜拉欣, 我补充说,在我的Viewcontroller.m没有sucess,它刚开始的视频肖像玩。 。 ..我已经更新了我的ViewController.m中的代码原始questoin,如果它有帮助? 非常感谢您的时间! – tibewww

+0

好吧,当我进入视图控制器时出现消息是:) 但无论如何,它只出现一个我已经点击到其中一个按钮:(? 并使它一旦消失在ladnscape? 它应该采取以下路径: 当我到达我的视图控制器,我点击一个按钮,此按钮开始播放视频这个按钮的点击,如果用户是在肖像,它应该显示此消息,并作出。 感谢您的全部时间;) – tibewww

+0

请检查我的更新回答 –