VideoPlayerSampleViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 2012 IGN Entertainment, Inc. */
  2. #import "VideoPlayerSampleViewController.h"
  3. #import "VideoPlayerSampleView.h"
  4. #define LABEL_PADDING 10
  5. #define TOPVIEW_HEIGHT 40
  6. @interface VideoPlayerSampleViewController ()
  7. @property (nonatomic, strong) VideoPlayerKit *videoPlayerViewController;
  8. @property (nonatomic, strong) UIView *topView;
  9. @property (nonatomic, strong) VideoPlayerSampleView *videoPlayerSampleView;
  10. @property (nonatomic) BOOL fullScreenOnOrientationChange;
  11. @property (nonatomic) BOOL isFullScreenPortraitOrientation;
  12. @end
  13. @implementation VideoPlayerSampleViewController
  14. - (id)init
  15. {
  16. if ((self = [super init])) {
  17. // Optional auto-fullscreen on orientation change
  18. self.fullScreenOnOrientationChange = YES;
  19. // Optional Top View
  20. _topView = [[UIView alloc] init];
  21. UILabel *topViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(LABEL_PADDING, 5, 200, 40.0)];
  22. topViewLabel.text = @"Top View Label";
  23. topViewLabel.textColor = [UIColor whiteColor];
  24. [_topView addSubview:topViewLabel];
  25. }
  26. return self;
  27. }
  28. - (void) handleOrientationChanged:(NSNotification *)note
  29. {
  30. UIDevice * device = note.object;
  31. switch(device.orientation)
  32. {
  33. case UIDeviceOrientationLandscapeLeft:
  34. case UIDeviceOrientationLandscapeRight:
  35. if (!self.videoPlayerViewController.fullScreenModeToggled) {
  36. [self.videoPlayerViewController launchFullScreen];
  37. } else if (self.videoPlayerViewController.allowPortraitFullscreen) {
  38. // Preserve portrait fullscreen mode
  39. self.isFullScreenPortraitOrientation = YES;
  40. }
  41. break;
  42. case UIDeviceOrientationPortrait:
  43. if (self.videoPlayerViewController.fullScreenModeToggled) {
  44. if (self.videoPlayerViewController.allowPortraitFullscreen &&
  45. self.isFullScreenPortraitOrientation) {
  46. // Reset the portrait mode flag
  47. self.isFullScreenPortraitOrientation = NO;
  48. } else {
  49. [self.videoPlayerViewController minimizeVideo];
  50. }
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. // Fullscreen / minimize without need for user's input
  58. - (void)fullScreen
  59. {
  60. if (!self.videoPlayerViewController.fullScreenModeToggled) {
  61. [self.videoPlayerViewController launchFullScreen];
  62. } else {
  63. [self.videoPlayerViewController minimizeVideo];
  64. }
  65. }
  66. - (void)loadView
  67. {
  68. self.videoPlayerSampleView = [[VideoPlayerSampleView alloc] init];
  69. [self.videoPlayerSampleView.playFullScreenButton addTarget:self action:@selector(playVideoFullScreen) forControlEvents:UIControlEventTouchUpInside];
  70. [self.videoPlayerSampleView.playInlineButton addTarget:self action:@selector(playVideoInline) forControlEvents:UIControlEventTouchUpInside];
  71. [self setView:self.videoPlayerSampleView];
  72. }
  73. - (void)playVideoFullScreen
  74. {
  75. // Hide Play Inline button on FullScreen to avoid layout conflicts
  76. [self.videoPlayerSampleView.playInlineButton setHidden:YES];
  77. [self playVideo:YES];
  78. }
  79. - (void)playVideoInline
  80. {
  81. [self playVideo:NO];
  82. }
  83. - (void)playVideo:(BOOL)playInFullScreen
  84. {
  85. NSURL *url = [NSURL URLWithString:@"https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8"];
  86. if (!self.videoPlayerViewController) {
  87. self.videoPlayerViewController = [VideoPlayerKit videoPlayerWithContainingView:self.videoPlayerSampleView.videoPlayerView optionalTopView:_topView hideTopViewWithControls:YES];
  88. // Need to set edge inset if top view is inserted
  89. [self.videoPlayerViewController setControlsEdgeInsets:UIEdgeInsetsMake(self.topView.frame.size.height, 0, 0, 0)];
  90. self.videoPlayerViewController.delegate = self;
  91. self.videoPlayerViewController.allowPortraitFullscreen = YES;
  92. } else {
  93. [self.videoPlayerViewController.view removeFromSuperview];
  94. }
  95. [self.view addSubview:self.videoPlayerViewController.view];
  96. [self.videoPlayerViewController playVideoWithTitle:@"Video Title" URL:url videoID:nil shareURL:nil isStreaming:NO playInFullScreen:playInFullScreen];
  97. }
  98. - (void)viewDidLoad
  99. {
  100. [super viewDidLoad];
  101. self.topView.frame = CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height, self.view.bounds.size.width, TOPVIEW_HEIGHT);
  102. if (self.fullScreenOnOrientationChange) {
  103. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  104. [[NSNotificationCenter defaultCenter]
  105. addObserver:self
  106. selector:@selector(handleOrientationChanged:)
  107. name:UIDeviceOrientationDidChangeNotification
  108. object:[UIDevice currentDevice]];
  109. }
  110. }
  111. - (void)dealloc {
  112. [[NSNotificationCenter defaultCenter] removeObserver:self];
  113. }
  114. @end