VLCPlayerDisplayController.m 10.0 KB

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