VLCMovieViewController.m 11 KB

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