VLCMovieViewController.m 9.8 KB

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