VLCPlayerDisplayController.m 11 KB

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