VLCPlayerDisplayController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #import "VLCPlaybackController+MediaLibrary.h"
  17. #import "VLC-Swift.h"
  18. #if TARGET_OS_IOS
  19. #import "VLC-Swift.h"
  20. #import "VLCMovieViewController.h"
  21. #else
  22. #import "VLCFullscreenMovieTVViewController.h"
  23. #endif
  24. static NSString *const VLCPlayerDisplayControllerDisplayModeKey = @"VLCPlayerDisplayControllerDisplayMode";
  25. @interface VLCUntouchableView: UIView
  26. @end
  27. @implementation VLCUntouchableView
  28. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  29. {
  30. UIView *result = [super hitTest:point withEvent:event];
  31. return result == self ? nil : result;
  32. }
  33. @end
  34. @interface VLCPlayerDisplayController () <VLCMovieViewControllerDelegate>
  35. @property (nonatomic, strong) UIViewController<VLCPlaybackControllerDelegate> *movieViewController;
  36. @property (nonatomic, strong) UIView<VLCPlaybackControllerDelegate, VLCMiniPlaybackViewInterface> *miniPlaybackView;
  37. @property (nonatomic, strong) NSLayoutConstraint *bottomConstraint;
  38. @property (nonatomic, strong) VLCService *services;
  39. @end
  40. @implementation VLCPlayerDisplayController
  41. - (instancetype)initWithServices:(id)services
  42. {
  43. self = [super initWithNibName:nil bundle:nil];
  44. if (self) {
  45. NSAssert([services isKindOfClass:[VLCService class]], @"VLCPlayerDisplayController: Injected services class issue");
  46. _services = services;
  47. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  48. [notificationCenter addObserver:self selector:@selector(playbackDidStart:) name:VLCPlaybackControllerPlaybackDidStart object:nil];
  49. [notificationCenter addObserver:self selector:@selector(playbackDidFail:) name:VLCPlaybackControllerPlaybackDidFail object:nil];
  50. [notificationCenter addObserver:self selector:@selector(playbackDidStop:) name:VLCPlaybackControllerPlaybackDidStop object:nil];
  51. [[NSUserDefaults standardUserDefaults] registerDefaults:@{VLCPlayerDisplayControllerDisplayModeKey : @(VLCPlayerDisplayControllerDisplayModeFullscreen)}];
  52. }
  53. return self;
  54. }
  55. - (void)viewDidLoad
  56. {
  57. self.view = [[VLCUntouchableView alloc] initWithFrame:self.view.frame];
  58. }
  59. #pragma mark - properties
  60. - (VLCPlayerDisplayControllerDisplayMode)displayMode
  61. {
  62. return [[NSUserDefaults standardUserDefaults] integerForKey:VLCPlayerDisplayControllerDisplayModeKey];
  63. }
  64. - (void)setDisplayMode:(VLCPlayerDisplayControllerDisplayMode)displayMode
  65. {
  66. [[NSUserDefaults standardUserDefaults] setInteger:displayMode forKey:VLCPlayerDisplayControllerDisplayModeKey];
  67. }
  68. - (VLCPlaybackController *)playbackController {
  69. if (_playbackController == nil) {
  70. _playbackController = [VLCPlaybackController sharedInstance];
  71. }
  72. return _playbackController;
  73. }
  74. - (UIViewController<VLCPlaybackControllerDelegate> *)movieViewController
  75. {
  76. if (!_movieViewController) {
  77. #if TARGET_OS_IOS
  78. _movieViewController = [[VLCMovieViewController alloc] initWithServices:_services];
  79. ((VLCMovieViewController *)_movieViewController).delegate = self;
  80. #else
  81. _movieViewController = [[VLCFullscreenMovieTVViewController alloc] initWithNibName:nil bundle:nil];
  82. #endif
  83. self.playbackController.delegate = _movieViewController;
  84. }
  85. return _movieViewController;
  86. }
  87. #pragma mark - Notification Handling
  88. - (void)playbackDidStart:(NSNotification *)notification
  89. {
  90. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  91. BOOL enforceFullscreen = [[defaults objectForKey:kVLCSettingVideoFullscreenPlayback] boolValue];
  92. if (self.playbackController.fullscreenSessionRequested && enforceFullscreen) {
  93. [self showFullscreenPlayback];
  94. return;
  95. }
  96. switch (self.displayMode) {
  97. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  98. [self _presentFullscreenPlaybackViewIfNeeded];
  99. break;
  100. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  101. [self _showHideMiniPlaybackView];
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. - (void)playbackDidStop:(NSNotification *)notification
  108. {
  109. [self dismissPlaybackView];
  110. }
  111. - (void)playbackDidFail:(NSNotification *)notification
  112. {
  113. [self showPlaybackError];
  114. }
  115. #pragma mark - API
  116. - (void)showFullscreenPlayback
  117. {
  118. self.displayMode = VLCPlayerDisplayControllerDisplayModeFullscreen;
  119. [self _presentFullscreenPlaybackViewIfNeeded];
  120. }
  121. - (void)closeFullscreenPlayback
  122. {
  123. [self.movieViewController dismissViewControllerAnimated:[self shouldAnimate] completion:nil];
  124. self.displayMode = VLCPlayerDisplayControllerDisplayModeMiniplayer;
  125. [self _showHideMiniPlaybackView];
  126. }
  127. #pragma mark - presentation handling
  128. - (BOOL)shouldAnimate
  129. {
  130. return [[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground;
  131. }
  132. - (void)pushPlaybackView
  133. {
  134. switch (self.displayMode) {
  135. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  136. [self _presentFullscreenPlaybackViewIfNeeded];
  137. break;
  138. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  139. [self _showHideMiniPlaybackView];
  140. default:
  141. break;
  142. }
  143. }
  144. - (void)dismissPlaybackView
  145. {
  146. switch (self.displayMode) {
  147. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  148. [self _closeFullscreenPlayback];
  149. break;
  150. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  151. [self _showHideMiniPlaybackView];
  152. default:
  153. break;
  154. }
  155. }
  156. - (void)showPlaybackError
  157. {
  158. NSString *failedString = NSLocalizedString(@"PLAYBACK_FAILED", nil);
  159. #if TARGET_OS_IOS
  160. switch (self.displayMode) {
  161. case VLCPlayerDisplayControllerDisplayModeFullscreen:
  162. if ([self.movieViewController respondsToSelector:@selector(showStatusMessage:forPlaybackController:)]) {
  163. [self.movieViewController showStatusMessage:failedString forPlaybackController:nil];
  164. }
  165. break;
  166. case VLCPlayerDisplayControllerDisplayModeMiniplayer:
  167. default:
  168. [VLCAlertViewController alertViewManagerWithTitle:failedString
  169. errorMessage:nil
  170. viewController:self];
  171. break;
  172. }
  173. #else
  174. UIAlertController *alert = [UIAlertController alertControllerWithTitle:failedString
  175. message:@""
  176. preferredStyle:UIAlertControllerStyleAlert];
  177. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_OK", nil)
  178. style:UIAlertActionStyleDefault
  179. handler:^(UIAlertAction * action) {}];
  180. [alert addAction:defaultAction];
  181. [self presentViewController:alert animated:YES completion:nil];
  182. #endif
  183. }
  184. #pragma mark - fullscreen player
  185. - (void)_presentFullscreenPlaybackViewIfNeeded
  186. {
  187. dispatch_async(dispatch_get_main_queue(), ^{
  188. if (!self.movieViewController.presentingViewController) {
  189. [self _presentMovieViewControllerAnimated:[self shouldAnimate]];
  190. }
  191. });
  192. }
  193. - (void)_closeFullscreenPlayback
  194. {
  195. dispatch_async(dispatch_get_main_queue(), ^{
  196. BOOL animated = [self shouldAnimate];
  197. [self.movieViewController dismissViewControllerAnimated:animated completion:nil];
  198. [self _showHideMiniPlaybackView];
  199. });
  200. }
  201. - (void)_presentMovieViewControllerAnimated:(BOOL)animated
  202. {
  203. UIViewController<VLCPlaybackControllerDelegate> *movieViewController = self.movieViewController;
  204. UINavigationController *navCon = [[VLCPlaybackNavigationController alloc] initWithRootViewController:movieViewController];
  205. [movieViewController prepareForMediaPlayback:self.playbackController];
  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. #if TARGET_OS_TV
  213. return;
  214. #else
  215. if (![NSThread isMainThread]) {
  216. [self performSelectorOnMainThread:@selector(_showHideMiniPlaybackView) withObject:nil waitUntilDone:NO];
  217. return;
  218. }
  219. VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
  220. UIView<VLCPlaybackControllerDelegate, VLCMiniPlaybackViewInterface> *miniPlaybackView = self.miniPlaybackView;
  221. const NSTimeInterval animationDuration = 0.25;
  222. const BOOL activePlaybackSession = playbackController.isPlaying || playbackController.willPlay;
  223. const BOOL miniPlayerVisible = miniPlaybackView.visible;
  224. BOOL needsShow = activePlaybackSession && !miniPlayerVisible;
  225. BOOL needsHide = !activePlaybackSession && miniPlayerVisible;
  226. if (self.editing) {
  227. needsHide = YES;
  228. needsShow = NO;
  229. }
  230. void (^completionBlock)(BOOL) = nil;
  231. if (needsShow) {
  232. if (!miniPlaybackView) {
  233. self.miniPlaybackView = miniPlaybackView = [[VLCMiniPlaybackView alloc] initWithFrame:CGRectZero];
  234. miniPlaybackView.translatesAutoresizingMaskIntoConstraints = NO;
  235. miniPlaybackView.userInteractionEnabled = YES;
  236. [self.view addSubview:miniPlaybackView];
  237. _bottomConstraint = [miniPlaybackView.topAnchor constraintEqualToAnchor:self.view.bottomAnchor];
  238. [NSLayoutConstraint activateConstraints:
  239. @[_bottomConstraint,
  240. [miniPlaybackView.heightAnchor constraintEqualToConstant:60.0],
  241. [miniPlaybackView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  242. [miniPlaybackView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  243. ]];
  244. [self.view layoutIfNeeded];
  245. }
  246. miniPlaybackView.visible = YES;
  247. } else if (needsHide) {
  248. miniPlaybackView.visible = NO;
  249. completionBlock = ^(BOOL finished) {
  250. UIView<VLCPlaybackControllerDelegate, VLCMiniPlaybackViewInterface> *miniPlaybackView = self.miniPlaybackView;
  251. if (miniPlaybackView.visible == NO) {
  252. [miniPlaybackView removeFromSuperview];
  253. self.miniPlaybackView = nil;
  254. }
  255. };
  256. }
  257. //when switching between tableview and collectionview all subviews are removed, make sure to readd it when this happens
  258. if (!miniPlaybackView.superview && miniPlayerVisible) {
  259. [self.view addSubview:miniPlaybackView];
  260. }
  261. // either way update view
  262. [miniPlaybackView prepareForMediaPlayback:playbackController];
  263. if (needsShow || needsHide) {
  264. [UIView animateWithDuration:animationDuration
  265. delay:animationDuration
  266. options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
  267. animations:^{
  268. self.bottomConstraint.constant = needsHide ? 0 : -self->_miniPlaybackView.frame.size.height -self.view.layoutMargins.bottom;
  269. [self.view layoutIfNeeded];
  270. }
  271. completion:completionBlock];
  272. }
  273. #endif
  274. }
  275. #pragma mark - MovieViewControllerDelegate
  276. - (void)movieViewControllerDidSelectMinimize:(VLCMovieViewController *)movieViewController
  277. {
  278. [self closeFullscreenPlayback];
  279. }
  280. - (BOOL)movieViewControllerShouldBeDisplayed:(VLCMovieViewController *)movieViewController
  281. {
  282. return self.displayMode == VLCPlayerDisplayControllerDisplayModeFullscreen;
  283. }
  284. @end