VLCPlayerDisplayController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*****************************************************************************
  2. * VLCPlayerDisplayController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCPlayerDisplayController.h"
  13. #import "VLCPlaybackController.h"
  14. #import "VLCPlaybackNavigationController.h"
  15. #import "VLCMovieViewController.h"
  16. #import "VLCMiniPlaybackView.h"
  17. static NSString *const VLCPlayerDisplayControllerDisplayModeKey = @"VLCPlayerDisplayControllerDisplayMode";
  18. @interface VLCPlayerDisplayController ()
  19. @property (nonatomic, strong) VLCMovieViewController *movieViewController;
  20. @property (nonatomic, strong) VLCMiniPlaybackView *miniPlaybackView;
  21. @end
  22. @implementation VLCPlayerDisplayController
  23. + (void)initialize
  24. {
  25. [[NSUserDefaults standardUserDefaults] registerDefaults:@{VLCPlayerDisplayControllerDisplayModeKey : @(VLCPlayerDisplayControllerDisplayModeFullscreen)}];
  26. }
  27. static inline void commonSetup(VLCPlayerDisplayController *self)
  28. {
  29. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  30. [notificationCenter addObserver:self selector:@selector(playbackDidStart:) name:VLCPlaybackControllerPlaybackDidStart object:nil];
  31. [notificationCenter addObserver:self selector:@selector(playbackDidFail:) name:VLCPlaybackControllerPlaybackDidFail object:nil];
  32. [notificationCenter addObserver:self selector:@selector(playbackDidStop:) name:VLCPlaybackControllerPlaybackDidStop object:nil];
  33. }
  34. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  35. {
  36. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  37. if (self) {
  38. commonSetup(self);
  39. }
  40. return self;
  41. }
  42. - (void)awakeFromNib
  43. {
  44. [super awakeFromNib];
  45. commonSetup(self);
  46. }
  47. - (void)dealloc
  48. {
  49. [[NSNotificationCenter defaultCenter] removeObserver:self];
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. [self setupChildViewController];
  55. }
  56. #pragma mark - ChildViewController
  57. - (void)setChildViewController:(UIViewController *)childViewController
  58. {
  59. if (_childViewController) {
  60. [_childViewController willMoveToParentViewController:nil];
  61. [_childViewController.view removeFromSuperview];
  62. [_childViewController removeFromParentViewController];
  63. }
  64. _childViewController = childViewController;
  65. if (self.isViewLoaded) {
  66. [self setupChildViewController];
  67. }
  68. }
  69. - (void)setupChildViewController
  70. {
  71. UIViewController *childViewController = self.childViewController;
  72. [self addChildViewController:childViewController];
  73. [self.view addSubview:childViewController.view];
  74. [childViewController didMoveToParentViewController:self];
  75. }
  76. #pragma mark - properties
  77. - (VLCPlayerDisplayControllerDisplayMode)displayMode
  78. {
  79. return [[NSUserDefaults standardUserDefaults] integerForKey:VLCPlayerDisplayControllerDisplayModeKey];
  80. }
  81. - (void)setDisplayMode:(VLCPlayerDisplayControllerDisplayMode)displayMode
  82. {
  83. [[NSUserDefaults standardUserDefaults] setInteger:displayMode forKey:VLCPlayerDisplayControllerDisplayModeKey];
  84. }
  85. - (VLCPlaybackController *)playbackController {
  86. if (_playbackController == nil) {
  87. _playbackController = [VLCPlaybackController sharedInstance];
  88. }
  89. return _playbackController;
  90. }
  91. - (VLCMovieViewController *)movieViewController
  92. {
  93. if (!_movieViewController) {
  94. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  95. [VLCPlaybackController sharedInstance].delegate = _movieViewController;
  96. }
  97. return _movieViewController;
  98. }
  99. #pragma mark - Notification Handling
  100. - (void)playbackDidStart:(NSNotification *)notification
  101. {
  102. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  103. BOOL enforceFullscreen = [[defaults objectForKey:kVLCSettingVideoFullscreenPlayback] boolValue];
  104. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  105. if (vpc.fullscreenSessionRequested && enforceFullscreen) {
  106. [self showFullscreenPlayback];
  107. return;
  108. }
  109. switch (self.displayMode) {
  110. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  111. [self _presentFullscreenPlaybackViewIfNeeded];
  112. break;
  113. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  114. [self _showHideMiniPlaybackView];
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. - (void)playbackDidStop:(NSNotification *)notification
  121. {
  122. [self dismissPlaybackView];
  123. }
  124. - (void)playbackDidFail:(NSNotification *)notification
  125. {
  126. [self showPlaybackError];
  127. }
  128. #pragma mark - API
  129. - (void)showFullscreenPlayback
  130. {
  131. self.displayMode = VLCPlayerDisplayControllerDisplayModeFullscreen;
  132. [self _presentFullscreenPlaybackViewIfNeeded];
  133. }
  134. - (void)closeFullscreenPlayback
  135. {
  136. [self _closeFullscreenPlayback];
  137. self.displayMode = VLCPlayerDisplayControllerDisplayModeMiniplayer;
  138. [self _showHideMiniPlaybackView];
  139. }
  140. #pragma mark - presentation handling
  141. - (BOOL)shouldAnimate
  142. {
  143. return [[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground;
  144. }
  145. - (void)pushPlaybackView
  146. {
  147. switch (self.displayMode) {
  148. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  149. [self _presentFullscreenPlaybackViewIfNeeded];
  150. break;
  151. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  152. [self _showHideMiniPlaybackView];
  153. default:
  154. break;
  155. }
  156. }
  157. - (void)dismissPlaybackView
  158. {
  159. switch (self.displayMode) {
  160. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  161. [self _closeFullscreenPlayback];
  162. break;
  163. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  164. [self _showHideMiniPlaybackView];
  165. default:
  166. break;
  167. }
  168. }
  169. - (void)showPlaybackError
  170. {
  171. NSString *failedString = NSLocalizedString(@"PLAYBACK_FAILED", nil);
  172. switch (self.displayMode) {
  173. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  174. [self.movieViewController showStatusMessage:failedString];
  175. break;
  176. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  177. default:
  178. [[[VLCAlertView alloc] initWithTitle:failedString
  179. message:nil
  180. delegate:nil
  181. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  182. otherButtonTitles:nil] show];
  183. break;
  184. }
  185. }
  186. #pragma mark - fullscreen player
  187. - (void)_presentFullscreenPlaybackViewIfNeeded
  188. {
  189. if (!self.movieViewController.presentingViewController) {
  190. [self _presentMovieViewControllerAnimated:[self shouldAnimate]];
  191. }
  192. }
  193. - (void)_closeFullscreenPlayback
  194. {
  195. BOOL animated = [self shouldAnimate];
  196. [self.movieViewController setControlsHidden:YES animated:animated];
  197. [self.movieViewController dismissViewControllerAnimated:animated completion:nil];
  198. [self _showHideMiniPlaybackView];
  199. }
  200. - (void)_presentMovieViewControllerAnimated:(BOOL)animated
  201. {
  202. VLCMovieViewController *movieViewController = self.movieViewController;
  203. UINavigationController *navCon = [[VLCPlaybackNavigationController alloc] initWithRootViewController:movieViewController];
  204. [movieViewController prepareForMediaPlayback:self.playbackController];
  205. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  206. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  207. [window.rootViewController presentViewController:navCon animated:animated completion:nil];
  208. }
  209. #pragma mark - miniplayer
  210. - (void)_showHideMiniPlaybackView
  211. {
  212. VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
  213. VLCMiniPlaybackView *miniPlaybackView = self.miniPlaybackView;
  214. const NSTimeInterval animationDuration = 0.25;
  215. const BOOL activePlaybackSession = playbackController.activePlaybackSession;
  216. const BOOL miniPlayerVisible = miniPlaybackView.visible;
  217. const CGRect viewRect = self.view.bounds;
  218. const CGFloat miniPlayerHeight = 60.;
  219. const CGRect miniPlayerFrameIn = CGRectMake(0., viewRect.size.height-miniPlayerHeight, viewRect.size.width, miniPlayerHeight);
  220. const CGRect miniPlayerFrameOut = CGRectMake(0., viewRect.size.height, viewRect.size.width, miniPlayerHeight);
  221. BOOL needsShow = activePlaybackSession && !miniPlayerVisible;
  222. BOOL needsHide = !activePlaybackSession && miniPlayerVisible;
  223. if (self.editing) {
  224. needsHide = YES;
  225. needsShow = NO;
  226. }
  227. void (^completionBlock)(BOOL) = nil;
  228. if (needsShow) {
  229. if (!miniPlaybackView) {
  230. self.miniPlaybackView = miniPlaybackView = [[VLCMiniPlaybackView alloc] initWithFrame:miniPlayerFrameOut];
  231. miniPlaybackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  232. [self.view addSubview:miniPlaybackView];
  233. }
  234. miniPlaybackView.visible = YES;
  235. } else if (needsHide) {
  236. miniPlaybackView.visible = NO;
  237. completionBlock = ^(BOOL finished) {
  238. VLCMiniPlaybackView *miniPlaybackView = self.miniPlaybackView;
  239. if (miniPlaybackView.visible == NO) {
  240. [miniPlaybackView removeFromSuperview];
  241. self.miniPlaybackView = nil;
  242. }
  243. };
  244. }
  245. //when switching between tableview and collectionview all subviews are removed, make sure to readd it when this happens
  246. if (!miniPlaybackView.superview && miniPlayerVisible) {
  247. [self.view addSubview:miniPlaybackView];
  248. }
  249. // either way update view
  250. [miniPlaybackView setupForWork:playbackController];
  251. if (needsShow || needsHide) {
  252. UIViewController *childViewController = self.childViewController;
  253. const CGRect newMiniPlayerFrame = needsHide ? miniPlayerFrameOut : miniPlayerFrameIn;
  254. CGRect newChildViewFrame = childViewController.view.frame;
  255. newChildViewFrame.size.height = CGRectGetMinY(newMiniPlayerFrame)-CGRectGetMinY(newChildViewFrame);
  256. [UIView animateWithDuration:animationDuration
  257. delay:animationDuration
  258. options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
  259. animations:^{
  260. miniPlaybackView.frame = newMiniPlayerFrame;
  261. childViewController.view.frame = newChildViewFrame;
  262. }
  263. completion:completionBlock];
  264. }
  265. }
  266. @end