VLCPlayerDisplayController.m 11 KB

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