VLCMovieViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // VLCMovieViewController.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCMovieViewController.h"
  9. #import "VLCExternalDisplayController.h"
  10. @interface VLCMovieViewController () <UIGestureRecognizerDelegate>
  11. @property (nonatomic, retain) UIPopoverController *masterPopoverController;
  12. @property (nonatomic, retain) UIWindow *externalWindow;
  13. @end
  14. @implementation VLCMovieViewController
  15. @synthesize movieView=_movieView, backButton=_backButton, positionSlider=_positionSlider, timeDisplay=_timeDisplay, playPauseButton = _playPauseButton, bwdButton = _bwdButton, fwdButton = _fwdButton, subtitleSwitcherButton = _subtitleSwitcherButton, audioSwitcherButton = _audioSwitcherButton;
  16. @synthesize toolbar = _toolbar, controllerPanel = _controllerPanel;
  17. - (void)dealloc
  18. {
  19. [_mediaItem release];
  20. [_masterPopoverController release];
  21. [_externalWindow release];
  22. [_toolbar release];
  23. [_movieView release];
  24. [_backButton release];
  25. [_positionSlider release];
  26. [_timeDisplay release];
  27. [_playPauseButton release];
  28. [_bwdButton release];
  29. [_fwdButton release];
  30. [_subtitleActionSheet release];
  31. [_audioSwitcherButton release];
  32. [_controllerPanel release];
  33. [[NSNotificationCenter defaultCenter] removeObserver:self];
  34. [super dealloc];
  35. }
  36. #pragma mark - Managing the media item
  37. - (void)setMediaItem:(id)newMediaItem
  38. {
  39. if (_mediaItem != newMediaItem) {
  40. [_mediaItem release];
  41. _mediaItem = [newMediaItem retain];
  42. }
  43. if (self.masterPopoverController != nil)
  44. [self.masterPopoverController dismissPopoverAnimated:YES];
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. self.wantsFullScreenLayout = YES;
  50. _mediaPlayer = [[VLCMediaPlayer alloc] init];
  51. [_mediaPlayer setDelegate:self];
  52. [_mediaPlayer setDrawable:self.movieView];
  53. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  54. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  55. name:UIScreenDidConnectNotification object:nil];
  56. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  57. name:UIScreenDidDisconnectNotification object:nil];
  58. if ([self hasExternalDisplay]) {
  59. [self showOnExternalDisplay];
  60. }
  61. _movieView.userInteractionEnabled = NO;
  62. UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toogleControlsVisible)];
  63. recognizer.delegate = self;
  64. [self.view addGestureRecognizer:recognizer];
  65. [recognizer release];
  66. }
  67. - (void)viewWillAppear:(BOOL)animated
  68. {
  69. [super viewWillAppear:animated];
  70. [self.navigationController setNavigationBarHidden:YES animated:YES];
  71. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
  72. if (self.mediaItem) {
  73. self.title = [self.mediaItem title];
  74. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:self.mediaItem.url]]];
  75. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < 0.99)
  76. [_mediaPlayer setPosition:[self.mediaItem.lastPosition floatValue]];
  77. [_mediaPlayer play];
  78. }
  79. }
  80. - (void)viewWillDisappear:(BOOL)animated
  81. {
  82. [self.navigationController setNavigationBarHidden:NO animated:YES];
  83. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  84. [_mediaPlayer pause];
  85. [super viewWillDisappear:animated];
  86. }
  87. - (void)didReceiveMemoryWarning
  88. {
  89. [super didReceiveMemoryWarning];
  90. // Dispose of any resources that can be recreated.
  91. }
  92. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  93. {
  94. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  95. if (self)
  96. self.title = @"Video Playback";
  97. return self;
  98. }
  99. #pragma mark - controls visibility
  100. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  101. {
  102. if (touch.view != self.view) {
  103. return NO;
  104. }
  105. return YES;
  106. }
  107. - (void)toogleControlsVisible
  108. {
  109. _controlsHidden = !_controlsHidden;
  110. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  111. if (!_controlsHidden) {
  112. _controllerPanel.alpha = 0.0f;
  113. _controllerPanel.hidden = NO;
  114. _toolbar.alpha = 0.0f;
  115. _toolbar.hidden = NO;
  116. }
  117. void (^animationBlock)() = ^() {
  118. _controllerPanel.alpha = alpha;
  119. _toolbar.alpha = alpha;
  120. };
  121. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  122. _controllerPanel.hidden = _controlsHidden;
  123. _toolbar.hidden = _controlsHidden;
  124. };
  125. [UIView animateWithDuration:0.3f animations:animationBlock completion:completionBlock];
  126. }
  127. #pragma mark - controls
  128. - (IBAction)closePlayback:(id)sender
  129. {
  130. [self.navigationController popViewControllerAnimated:YES];
  131. }
  132. - (IBAction)positionSliderAction:(UISlider *)sender
  133. {
  134. _mediaPlayer.position = sender.value;
  135. }
  136. - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification {
  137. self.positionSlider.value = [_mediaPlayer position];
  138. self.timeDisplay.title = [[_mediaPlayer remainingTime] stringValue];
  139. }
  140. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  141. {
  142. // TODO
  143. }
  144. - (IBAction)play:(id)sender
  145. {
  146. if ([_mediaPlayer isPlaying]) {
  147. [_mediaPlayer pause];
  148. _playPauseButton.titleLabel.text = @"Pse";
  149. } else {
  150. [_mediaPlayer play];
  151. _playPauseButton.titleLabel.text = @"Play";
  152. }
  153. }
  154. - (IBAction)forward:(id)sender
  155. {
  156. [_mediaPlayer mediumJumpForward];
  157. }
  158. - (IBAction)backward:(id)sender
  159. {
  160. [_mediaPlayer mediumJumpBackward];
  161. }
  162. - (IBAction)switchAudioTrack:(id)sender
  163. {
  164. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Audio Track", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  165. NSArray * audioTracks = [_mediaPlayer audioTrackNames];
  166. NSUInteger count = [audioTracks count];
  167. for (NSUInteger i = 0; i < count; i++)
  168. [_audiotrackActionSheet addButtonWithTitle:[audioTracks objectAtIndex:i]];
  169. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"audio track selector")];
  170. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  171. [_audiotrackActionSheet showFromRect:[self.audioSwitcherButton frame] inView:self.audioSwitcherButton animated:YES];
  172. }
  173. - (IBAction)switchSubtitleTrack:(id)sender
  174. {
  175. NSArray * spuTracks = [_mediaPlayer videoSubTitlesNames];
  176. NSUInteger count = [spuTracks count];
  177. if (count <= 1)
  178. return;
  179. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Subtitle Track", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  180. for (NSUInteger i = 0; i < count; i++)
  181. [_subtitleActionSheet addButtonWithTitle:[spuTracks objectAtIndex:i]];
  182. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"subtitle track selector")];
  183. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  184. [_subtitleActionSheet showFromRect:[self.subtitleSwitcherButton frame] inView:self.subtitleSwitcherButton animated:YES];
  185. }
  186. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  187. NSUInteger arrayIndex = 0;
  188. NSArray * indexArray;
  189. NSArray * namesArray;
  190. if (actionSheet == _subtitleActionSheet) {
  191. namesArray = _mediaPlayer.videoSubTitlesNames;
  192. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  193. if (arrayIndex != NSNotFound) {
  194. indexArray = _mediaPlayer.videoSubTitlesIndexes;
  195. _mediaPlayer.currentVideoSubTitleIndex = [[indexArray objectAtIndex:arrayIndex] intValue];
  196. }
  197. [_subtitleActionSheet release];
  198. } else {
  199. namesArray = _mediaPlayer.audioTrackNames;
  200. arrayIndex = [namesArray indexOfObject:[actionSheet buttonTitleAtIndex:buttonIndex]];
  201. if (arrayIndex != NSNotFound) {
  202. indexArray = _mediaPlayer.audioTrackIndexes;
  203. _mediaPlayer.currentAudioTrackIndex = [[indexArray objectAtIndex:arrayIndex] intValue];
  204. }
  205. [_audiotrackActionSheet release];
  206. }
  207. }
  208. #pragma mark - Split view
  209. - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
  210. {
  211. barButtonItem.title = NSLocalizedString(@"Master", @"Master");
  212. [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
  213. self.masterPopoverController = popoverController;
  214. }
  215. - (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
  216. {
  217. // Called when the view is shown again in the split view, invalidating the button and popover controller.
  218. [self.navigationItem setLeftBarButtonItem:nil animated:YES];
  219. self.masterPopoverController = nil;
  220. }
  221. #pragma mark - External Display
  222. - (BOOL)hasExternalDisplay
  223. {
  224. return ([[UIScreen screens] count] > 1);
  225. }
  226. - (void)showOnExternalDisplay
  227. {
  228. UIScreen *screen = [[UIScreen screens] objectAtIndex:1];
  229. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  230. self.externalWindow = [[[UIWindow alloc] initWithFrame:screen.bounds] autorelease];
  231. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  232. self.externalWindow.rootViewController = controller;
  233. [controller.view addSubview:_movieView];
  234. controller.view.frame = screen.bounds;
  235. _movieView.frame = screen.bounds;
  236. [controller release];
  237. self.externalWindow.screen = screen;
  238. self.externalWindow.hidden = NO;
  239. }
  240. - (void)hideFromExternalDisplay
  241. {
  242. [self.view addSubview:_movieView];
  243. [self.view sendSubviewToBack:_movieView];
  244. _movieView.frame = self.view.frame;
  245. self.externalWindow.hidden = YES;
  246. self.externalWindow = nil;
  247. }
  248. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  249. {
  250. [self showOnExternalDisplay];
  251. }
  252. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  253. {
  254. [self hideFromExternalDisplay];
  255. }
  256. @end