VLCMovieViewController.m 11 KB

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