VLCPlayerDisplayController.m 12 KB

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