VLCPlayerDisplayController.m 12 KB

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