VLCMovieViewController.m 11 KB

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