2016-11-15 54 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

請檢查我的更新回答 –