VLCMovieViewController.m 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. /*****************************************************************************
  2. * VLCMovieViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <caro # videolan.org>
  11. * Tobias Conradi <videolan # tobias-conradi.de>
  12. * Ahmad Harb <harb.dev.leb # gmail.com>
  13. * Fabio Ritrovato <sephiroth87 # videolan.org>
  14. * Pierre SAGASPE <pierre.sagaspe # me.com>
  15. * Filipe Cabecinhas <vlc # filcab dot net>
  16. * Marc Etcheverry <marc # taplightsoftware dot com>
  17. * Christopher Loessl <cloessl # x-berg dot de>
  18. * Sylver Bruneau <sylver.bruneau # gmail dot com>
  19. *
  20. * Refer to the COPYING file of the official project for license.
  21. *****************************************************************************/
  22. #import "VLCMovieViewController.h"
  23. #import "VLCExternalDisplayController.h"
  24. #import "VLCEqualizerView.h"
  25. #import "VLCMultiSelectionMenuView.h"
  26. #import "VLCPlaybackController.h"
  27. #import "UIDevice+VLC.h"
  28. #import "VLCTimeNavigationTitleView.h"
  29. #import "VLCAppDelegate.h"
  30. #import "VLCStatusLabel.h"
  31. #import "VLCMovieViewControlPanelView.h"
  32. #import "VLCSlider.h"
  33. #import "VLCTrackSelectorView.h"
  34. #import "VLCMetadata.h"
  35. #import "UIDevice+VLC.h"
  36. #import "VLC_iOS-Swift.h"
  37. #define FORWARD_SWIPE_DURATION 30
  38. #define BACKWARD_SWIPE_DURATION 10
  39. #define SHORT_JUMP_DURATION 10
  40. #define ZOOM_SENSITIVITY 2.99f
  41. #define DEFAULT_FOV 80.f
  42. #define MAX_FOV 150.f
  43. #define MIN_FOV 20.f
  44. typedef NS_ENUM(NSInteger, VLCPanType) {
  45. VLCPanTypeNone,
  46. VLCPanTypeBrightness,
  47. VLCPanTypeSeek,
  48. VLCPanTypeVolume,
  49. VLCPanTypeProjection
  50. };
  51. @interface VLCMovieViewController () <UIGestureRecognizerDelegate, VLCMultiSelectionViewDelegate, VLCEqualizerViewUIDelegate, VLCPlaybackControllerDelegate, VLCDeviceMotionDelegate>
  52. {
  53. BOOL _controlsHidden;
  54. BOOL _videoFiltersHidden;
  55. BOOL _playbackSpeedViewHidden;
  56. UIActionSheet *_subtitleActionSheet;
  57. UIActionSheet *_audiotrackActionSheet;
  58. NSTimer *_idleTimer;
  59. BOOL _viewAppeared;
  60. BOOL _displayRemainingTime;
  61. BOOL _positionSet;
  62. BOOL _isScrubbing;
  63. BOOL _interfaceIsLocked;
  64. BOOL _audioOnly;
  65. BOOL _volumeGestureEnabled;
  66. BOOL _playPauseGestureEnabled;
  67. BOOL _brightnessGestureEnabled;
  68. BOOL _seekGestureEnabled;
  69. BOOL _closeGestureEnabled;
  70. BOOL _variableJumpDurationEnabled;
  71. BOOL _playbackWillClose;
  72. BOOL _isTapSeeking;
  73. VLCMovieJumpState _previousJumpState;
  74. UIPinchGestureRecognizer *_pinchRecognizer;
  75. VLCPanType _currentPanType;
  76. UIPanGestureRecognizer *_panRecognizer;
  77. UISwipeGestureRecognizer *_swipeRecognizerLeft;
  78. UISwipeGestureRecognizer *_swipeRecognizerRight;
  79. UISwipeGestureRecognizer *_swipeRecognizerUp;
  80. UISwipeGestureRecognizer *_swipeRecognizerDown;
  81. UITapGestureRecognizer *_tapRecognizer;
  82. UITapGestureRecognizer *_tapOnVideoRecognizer;
  83. UITapGestureRecognizer *_tapToToggleiPhoneXRatioRecognizer;
  84. UITapGestureRecognizer *_tapToSeekRecognizer;
  85. UIButton *_doneButton;
  86. VLCTrackSelectorView *_trackSelectorContainer;
  87. VLCEqualizerView *_equalizerView;
  88. VLCMultiSelectionMenuView *_multiSelectionView;
  89. VLCPlaybackController *_vpc;
  90. UIView *_sleepTimerContainer;
  91. UIDatePicker *_sleepTimeDatePicker;
  92. NSTimer *_sleepCountDownTimer;
  93. NSInteger _mediaDuration;
  94. NSInteger _numberOfTapSeek;
  95. VLCDeviceMotion *_deviceMotion;
  96. CGFloat _fov;
  97. CGPoint _saveLocation;
  98. CGSize _screenPixelSize;
  99. }
  100. @property (nonatomic, strong) VLCMovieViewControlPanelView *controllerPanel;
  101. @property (nonatomic, strong) UIPopoverController *masterPopoverController;
  102. @property (nonatomic, strong) UIWindow *externalWindow;
  103. @end
  104. @implementation VLCMovieViewController
  105. + (void)initialize
  106. {
  107. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  108. NSDictionary *appDefaults = @{kVLCShowRemainingTime : @(YES)};
  109. [defaults registerDefaults:appDefaults];
  110. }
  111. - (void)viewDidLoad
  112. {
  113. [super viewDidLoad];
  114. CGRect rect;
  115. _vpc = [VLCPlaybackController sharedInstance];
  116. self.extendedLayoutIncludesOpaqueBars = YES;
  117. self.edgesForExtendedLayout = UIRectEdgeAll;
  118. self.videoFilterView.hidden = YES;
  119. _videoFiltersHidden = YES;
  120. _hueLabel.text = NSLocalizedString(@"VFILTER_HUE", nil);
  121. _hueSlider.accessibilityLabel = _hueLabel.text;
  122. _contrastLabel.text = NSLocalizedString(@"VFILTER_CONTRAST", nil);
  123. _contrastSlider.accessibilityLabel = _contrastLabel.text;
  124. _brightnessLabel.text = NSLocalizedString(@"VFILTER_BRIGHTNESS", nil);
  125. _brightnessSlider.accessibilityLabel = _brightnessLabel.text;
  126. _saturationLabel.text = NSLocalizedString(@"VFILTER_SATURATION", nil);
  127. _saturationSlider.accessibilityLabel = _saturationLabel.text;
  128. _gammaLabel.text = NSLocalizedString(@"VFILTER_GAMMA", nil);
  129. _gammaSlider.accessibilityLabel = _gammaLabel.text;
  130. _playbackSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SPEED", nil);
  131. _playbackSpeedSlider.accessibilityLabel = _playbackSpeedLabel.text;
  132. _audioDelayLabel.text = NSLocalizedString(@"AUDIO_DELAY", nil);
  133. _audioDelaySlider.accessibilityLabel = _audioDelayLabel.text;
  134. _spuDelayLabel.text = NSLocalizedString(@"SPU_DELAY", nil);
  135. _spuDelaySlider.accessibilityLabel = _spuDelayLabel.text;
  136. _resetVideoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER_RESET_BUTTON", nil);
  137. _sleepTimerButton.accessibilityLabel = NSLocalizedString(@"BUTTON_SLEEP_TIMER", nil);
  138. [_sleepTimerButton setTitle:NSLocalizedString(@"BUTTON_SLEEP_TIMER", nil) forState:UIControlStateNormal];
  139. _multiSelectionView = [[VLCMultiSelectionMenuView alloc] init];
  140. _multiSelectionView.delegate = self;
  141. _multiSelectionView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  142. _multiSelectionView.hidden = YES;
  143. [self.view addSubview:_multiSelectionView];
  144. _scrubHelpLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HELP", nil);
  145. self.playbackSpeedView.hidden = YES;
  146. _playbackSpeedViewHidden = YES;
  147. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  148. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  149. name:UIScreenDidConnectNotification object:nil];
  150. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  151. name:UIScreenDidDisconnectNotification object:nil];
  152. [center addObserver:self
  153. selector:@selector(appBecameActive:)
  154. name:UIApplicationDidBecomeActiveNotification
  155. object:nil];
  156. [center addObserver:self
  157. selector:@selector(playbackDidStop:)
  158. name:VLCPlaybackControllerPlaybackDidStop
  159. object:nil];
  160. _playingExternallyTitle.text = NSLocalizedString(@"PLAYING_EXTERNALLY_TITLE", nil);
  161. _playingExternallyDescription.text = NSLocalizedString(@"PLAYING_EXTERNALLY_DESC", nil);
  162. if ([[UIDevice currentDevice] VLCHasExternalDisplay])
  163. [self showOnExternalDisplay];
  164. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  165. _movieView.userInteractionEnabled = NO;
  166. [self setupGestureRecognizers];
  167. _isTapSeeking = NO;
  168. _previousJumpState = VLCMovieJumpStateDefault;
  169. _numberOfTapSeek = 0;
  170. rect = self.resetVideoFilterButton.frame;
  171. rect.origin.y = rect.origin.y + 5.;
  172. self.resetVideoFilterButton.frame = rect;
  173. [self.movieView setAccessibilityIdentifier:@"Video Player Title"];
  174. [self.movieView setAccessibilityLabel:NSLocalizedString(@"VO_VIDEOPLAYER_TITLE", nil)];
  175. [self.movieView setAccessibilityHint:NSLocalizedString(@"VO_VIDEOPLAYER_DOUBLETAP", nil)];
  176. _trackSelectorContainer = [[VLCTrackSelectorView alloc] initWithFrame:CGRectZero];
  177. _trackSelectorContainer.hidden = YES;
  178. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  179. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  180. [recognizer setEnabled:YES];
  181. _trackSelectorContainer.hidden = YES;
  182. };
  183. _trackSelectorContainer.completionHandler = completionBlock;
  184. _trackSelectorContainer.translatesAutoresizingMaskIntoConstraints = NO;
  185. [self.view addSubview:_trackSelectorContainer];
  186. _equalizerView = [[VLCEqualizerView alloc] initWithFrame:CGRectMake(0, 0, 450., 240.)];
  187. _equalizerView.delegate = [VLCPlaybackController sharedInstance];
  188. _equalizerView.UIdelegate = self;
  189. _equalizerView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  190. _equalizerView.hidden = YES;
  191. [self.view addSubview:_equalizerView];
  192. //Sleep Timer initialization
  193. [self sleepTimerInitializer];
  194. [self setupControlPanel];
  195. CGRect screenBounds = [[UIScreen mainScreen] bounds];
  196. CGFloat screenScale = [[UIScreen mainScreen] scale];
  197. _screenPixelSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
  198. [self setupConstraints];
  199. }
  200. - (void)setupGestureRecognizers
  201. {
  202. _tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];
  203. _tapOnVideoRecognizer.delegate = self;
  204. [self.view addGestureRecognizer:_tapOnVideoRecognizer];
  205. _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  206. _pinchRecognizer.delegate = self;
  207. if ([[UIDevice currentDevice] isiPhoneX]) {
  208. _tapToToggleiPhoneXRatioRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:_vpc action:@selector(switchIPhoneXFullScreen)];
  209. _tapToToggleiPhoneXRatioRecognizer.numberOfTapsRequired = 2;
  210. [self.view addGestureRecognizer:_tapToToggleiPhoneXRatioRecognizer];
  211. } else {
  212. _tapToSeekRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToSeekRecognized:)];
  213. [_tapToSeekRecognizer setNumberOfTapsRequired:2];
  214. [self.view addGestureRecognizer:_tapToSeekRecognizer];
  215. [_tapOnVideoRecognizer requireGestureRecognizerToFail:_tapToSeekRecognizer];
  216. }
  217. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togglePlayPause)];
  218. [_tapRecognizer setNumberOfTouchesRequired:2];
  219. _currentPanType = VLCPanTypeNone;
  220. _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
  221. [_panRecognizer setMinimumNumberOfTouches:1];
  222. [_panRecognizer setMaximumNumberOfTouches:1];
  223. _swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  224. _swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  225. _swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  226. _swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
  227. _swipeRecognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  228. _swipeRecognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
  229. _swipeRecognizerUp.numberOfTouchesRequired = 2;
  230. _swipeRecognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  231. _swipeRecognizerDown.direction = UISwipeGestureRecognizerDirectionDown;
  232. _swipeRecognizerDown.numberOfTouchesRequired = 2;
  233. [self.view addGestureRecognizer:_pinchRecognizer];
  234. [self.view addGestureRecognizer:_swipeRecognizerLeft];
  235. [self.view addGestureRecognizer:_swipeRecognizerRight];
  236. [self.view addGestureRecognizer:_swipeRecognizerUp];
  237. [self.view addGestureRecognizer:_swipeRecognizerDown];
  238. [self.view addGestureRecognizer:_panRecognizer];
  239. [self.view addGestureRecognizer:_tapRecognizer];
  240. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerLeft];
  241. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerRight];
  242. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerUp];
  243. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerDown];
  244. _panRecognizer.delegate = self;
  245. _swipeRecognizerRight.delegate = self;
  246. _swipeRecognizerLeft.delegate = self;
  247. _swipeRecognizerUp.delegate = self;
  248. _swipeRecognizerDown.delegate = self;
  249. _tapRecognizer.delegate = self;
  250. _tapToSeekRecognizer.delegate = self;
  251. }
  252. - (void)setupControlPanel
  253. {
  254. _controllerPanel = [[VLCMovieViewControlPanelView alloc] initWithFrame:CGRectZero];
  255. [_controllerPanel.bwdButton addTarget:self action:@selector(backward:) forControlEvents:UIControlEventTouchUpInside];
  256. [_controllerPanel.fwdButton addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];
  257. [_controllerPanel.playPauseButton addTarget:self action:@selector(playPause) forControlEvents:UIControlEventTouchUpInside];
  258. [_controllerPanel.moreActionsButton addTarget:self action:@selector(moreActions:) forControlEvents:UIControlEventTouchUpInside];
  259. [_controllerPanel.playbackSpeedButton addTarget:self action:@selector(showPlaybackSpeedView) forControlEvents:UIControlEventTouchUpInside];
  260. [_controllerPanel.trackSwitcherButton addTarget:self action:@selector(switchTrack:) forControlEvents:UIControlEventTouchUpInside];
  261. [_controllerPanel.videoFilterButton addTarget:self action:@selector(videoFilterToggle:) forControlEvents:UIControlEventTouchUpInside];
  262. // HACK: get the slider from volume view
  263. UISlider *volumeSlider = nil;
  264. for (id aView in _controllerPanel.volumeView.subviews){
  265. if ([aView isKindOfClass:[UISlider class]]){
  266. volumeSlider = (UISlider *)aView;
  267. break;
  268. }
  269. }
  270. [volumeSlider addTarget:self action:@selector(volumeSliderAction:) forControlEvents:UIControlEventValueChanged];
  271. _controllerPanel.translatesAutoresizingMaskIntoConstraints = NO;
  272. [self.view addSubview:_controllerPanel];
  273. }
  274. - (void)setupConstraints
  275. {
  276. NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[panel]|"
  277. options:0
  278. metrics:nil
  279. views:@{@"panel":_controllerPanel}];
  280. NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[panel]|"
  281. options:0
  282. metrics:nil
  283. views:@{@"panel":_controllerPanel}];
  284. [self.view addConstraints:hConstraints];
  285. [self.view addConstraints:vConstraints];
  286. //constraint within _trackSelectorContainer is setting it's height to the tableviews contentview
  287. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:2.0/3.0 constant:0];
  288. widthConstraint.priority = UILayoutPriorityRequired - 1;
  289. NSArray *constraints = @[
  290. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
  291. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  292. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:420.0],
  293. widthConstraint,
  294. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:2.0/3.0 constant:0],
  295. ];
  296. [NSLayoutConstraint activateConstraints:constraints];
  297. }
  298. - (void)setupNavigationbar
  299. {
  300. //Needs to be a UIButton since we need it to work with constraints
  301. _doneButton = [[UIButton alloc] initWithFrame:CGRectZero];
  302. [_doneButton setAccessibilityIdentifier:@"Done"];
  303. [_doneButton addTarget:self action:@selector(closePlayback:) forControlEvents:UIControlEventTouchUpInside];
  304. [_doneButton setTitle:NSLocalizedString(@"BUTTON_DONE", nil) forState:UIControlStateNormal];
  305. [_doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  306. _doneButton.translatesAutoresizingMaskIntoConstraints = NO;
  307. self.timeNavigationTitleView = [[[NSBundle mainBundle] loadNibNamed:@"VLCTimeNavigationTitleView" owner:self options:nil] objectAtIndex:0];
  308. self.timeNavigationTitleView.translatesAutoresizingMaskIntoConstraints = NO;
  309. [self.navigationController.navigationBar addSubview:self.timeNavigationTitleView];
  310. [self.navigationController.navigationBar addSubview:_doneButton];
  311. NSObject *guide = self.navigationController.navigationBar;
  312. if (@available(iOS 11.0, *)) {
  313. guide = self.navigationController.navigationBar.safeAreaLayoutGuide;
  314. }
  315. [self.navigationController.view addConstraints: @[
  316. [NSLayoutConstraint constraintWithItem:_doneButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeLeft multiplier:1 constant:8],
  317. [NSLayoutConstraint constraintWithItem:_doneButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  318. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_doneButton attribute:NSLayoutAttributeRight multiplier:1 constant:0],
  319. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeRight multiplier:1 constant:0],
  320. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  321. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  322. ]];
  323. }
  324. - (void)resetVideoFiltersSliders
  325. {
  326. _brightnessSlider.value = 1.;
  327. _contrastSlider.value = 1.;
  328. _hueSlider.value = 0.;
  329. _saturationSlider.value = 1.;
  330. _gammaSlider.value = 1.;
  331. }
  332. - (void)viewWillAppear:(BOOL)animated
  333. {
  334. [super viewWillAppear:animated];
  335. _vpc.delegate = self;
  336. [_vpc recoverPlaybackState];
  337. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  338. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  339. [self setupNavigationbar];
  340. /* reset audio meta data views */
  341. self.artworkImageView.image = nil;
  342. self.trackNameLabel.text = nil;
  343. self.artistNameLabel.text = nil;
  344. self.albumNameLabel.text = nil;
  345. [self setControlsHidden:NO animated:animated];
  346. [self updateDefaults];
  347. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDefaults) name:NSUserDefaultsDidChangeNotification object:nil];
  348. }
  349. - (void)viewDidAppear:(BOOL)animated
  350. {
  351. [super viewDidAppear:animated];
  352. _viewAppeared = YES;
  353. _playbackWillClose = NO;
  354. [_vpc recoverDisplayedMetadata];
  355. [self resetVideoFiltersSliders];
  356. _vpc.videoOutputView = self.movieView;
  357. _multiSelectionView.repeatMode = _vpc.repeatMode;
  358. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  359. //Media is loaded in the media player, checking the projection type and configuring accordingly.
  360. [self setupForMediaProjection];
  361. }
  362. - (void)viewDidLayoutSubviews
  363. {
  364. CGRect equalizerRect = _equalizerView.frame;
  365. equalizerRect.origin.x = CGRectGetMidX(self.view.bounds) - CGRectGetWidth(equalizerRect)/2.0;
  366. equalizerRect.origin.y = CGRectGetMidY(self.view.bounds) - CGRectGetHeight(equalizerRect)/2.0;
  367. _equalizerView.frame = equalizerRect;
  368. CGRect multiSelectionFrame;
  369. CGRect controllerPanelFrame = _controllerPanel.frame;;
  370. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
  371. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  372. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  373. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  374. _multiSelectionView.frame = multiSelectionFrame;
  375. _multiSelectionView.showsEqualizer = YES;
  376. return;
  377. }
  378. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
  379. _multiSelectionView.showsEqualizer = YES;
  380. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  381. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  382. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  383. } else {
  384. _multiSelectionView.showsEqualizer = NO;
  385. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  386. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  387. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  388. }
  389. _multiSelectionView.frame = multiSelectionFrame;
  390. }
  391. - (void)viewWillDisappear:(BOOL)animated
  392. {
  393. if (_vpc.videoOutputView == self.movieView) {
  394. _vpc.videoOutputView = nil;
  395. }
  396. _viewAppeared = NO;
  397. if (_idleTimer) {
  398. [_idleTimer invalidate];
  399. _idleTimer = nil;
  400. }
  401. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  402. [super viewWillDisappear:animated];
  403. // hide filter UI for next run
  404. if (!_videoFiltersHidden)
  405. _videoFiltersHidden = YES;
  406. if (_equalizerView.hidden == NO)
  407. _equalizerView.hidden = YES;
  408. if (!_playbackSpeedViewHidden)
  409. _playbackSpeedViewHidden = YES;
  410. if (_interfaceIsLocked)
  411. [self toggleUILock];
  412. // reset tap to seek values
  413. _isTapSeeking = NO;
  414. _previousJumpState = VLCMovieJumpStateDefault;
  415. _numberOfTapSeek = 0;
  416. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
  417. [[NSUserDefaults standardUserDefaults] setBool:_displayRemainingTime forKey:kVLCShowRemainingTime];
  418. }
  419. - (void)viewDidDisappear:(BOOL)animated
  420. {
  421. [super viewDidDisappear:animated];
  422. [self.deviceMotion stopDeviceMotion];
  423. }
  424. - (BOOL)canBecomeFirstResponder
  425. {
  426. return YES;
  427. }
  428. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
  429. {
  430. [self setControlsHidden:YES animated:flag];
  431. [super dismissViewControllerAnimated:flag completion:completion];
  432. }
  433. - (void)updateDefaults
  434. {
  435. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  436. if (!_playbackWillClose) {
  437. _displayRemainingTime = [[defaults objectForKey:kVLCShowRemainingTime] boolValue];
  438. [self updateTimeDisplayButton];
  439. }
  440. _volumeGestureEnabled = [[defaults objectForKey:kVLCSettingVolumeGesture] boolValue];
  441. _playPauseGestureEnabled = [[defaults objectForKey:kVLCSettingPlayPauseGesture] boolValue];
  442. _brightnessGestureEnabled = [[defaults objectForKey:kVLCSettingBrightnessGesture] boolValue];
  443. _seekGestureEnabled = [[defaults objectForKey:kVLCSettingSeekGesture] boolValue];
  444. _closeGestureEnabled = [[defaults objectForKey:kVLCSettingCloseGesture] boolValue];
  445. _variableJumpDurationEnabled = [[defaults objectForKey:kVLCSettingVariableJumpDuration] boolValue];
  446. }
  447. #pragma mark - Initializer helper
  448. - (void)sleepTimerInitializer
  449. {
  450. /* add sleep timer UI */
  451. _sleepTimerContainer = [[VLCFrostedGlasView alloc] initWithFrame:CGRectMake(0., 0., 300., 200.)];
  452. _sleepTimerContainer.center = self.view.center;
  453. _sleepTimerContainer.clipsToBounds = YES;
  454. _sleepTimerContainer.layer.cornerRadius = 5;
  455. _sleepTimerContainer.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  456. //Layers used to create a separator for the buttons.
  457. CALayer *horizontalSeparator = [CALayer layer];
  458. horizontalSeparator.frame = CGRectMake(0., 162., 300., 1.);
  459. horizontalSeparator.backgroundColor = [UIColor VLCLightTextColor].CGColor;
  460. CALayer *verticalSeparator = [CALayer layer];
  461. verticalSeparator.frame = CGRectMake(150., 162., 1., 48.);
  462. verticalSeparator.backgroundColor = [UIColor VLCLightTextColor].CGColor;
  463. _sleepTimeDatePicker = [[UIDatePicker alloc] init];
  464. _sleepTimeDatePicker.opaque = NO;
  465. _sleepTimeDatePicker.backgroundColor = [UIColor clearColor];
  466. _sleepTimeDatePicker.tintColor = [UIColor VLCLightTextColor];
  467. _sleepTimeDatePicker.frame = CGRectMake(0., 0., 300., 162.);
  468. _sleepTimeDatePicker.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  469. [_sleepTimerContainer addSubview:_sleepTimeDatePicker];
  470. [_sleepTimerContainer.layer addSublayer:horizontalSeparator];
  471. [_sleepTimerContainer.layer addSublayer:verticalSeparator];
  472. UIButton *cancelSleepTimer = [[UIButton alloc] initWithFrame:CGRectMake(0., 162., 150., 48.)];
  473. cancelSleepTimer.backgroundColor = [UIColor clearColor];
  474. [cancelSleepTimer setTitle:NSLocalizedString(@"BUTTON_RESET", nil) forState:UIControlStateNormal];
  475. [cancelSleepTimer setTintColor:[UIColor VLCLightTextColor]];
  476. [cancelSleepTimer setTitleColor:[UIColor VLCDarkTextShadowColor] forState:UIControlStateHighlighted];
  477. [cancelSleepTimer addTarget:self action:@selector(sleepTimerCancel:) forControlEvents:UIControlEventTouchDown];
  478. cancelSleepTimer.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
  479. [_sleepTimerContainer addSubview:cancelSleepTimer];
  480. UIButton *confirmSleepTimer = [[UIButton alloc] initWithFrame:CGRectMake(150., 162., 150., 48.)];
  481. confirmSleepTimer.backgroundColor = [UIColor clearColor];
  482. [confirmSleepTimer setTitle:NSLocalizedString(@"BUTTON_SET", nil) forState:UIControlStateNormal];
  483. [confirmSleepTimer setTintColor:[UIColor VLCLightTextColor]];
  484. [confirmSleepTimer setTitleColor:[UIColor VLCDarkTextShadowColor] forState:UIControlStateHighlighted];
  485. [confirmSleepTimer addTarget:self action:@selector(sleepTimerAction:) forControlEvents:UIControlEventTouchDown];
  486. confirmSleepTimer.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
  487. [_sleepTimerContainer addSubview:confirmSleepTimer];
  488. /* adapt the date picker style to suit our needs */
  489. [_sleepTimeDatePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"];
  490. SEL selector = NSSelectorFromString(@"setHighlightsToday:");
  491. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
  492. BOOL no = NO;
  493. [invocation setSelector:selector];
  494. [invocation setArgument:&no atIndex:2];
  495. [invocation invokeWithTarget:_sleepTimeDatePicker];
  496. if (_sleepTimerContainer.subviews.count > 0) {
  497. NSArray *subviewsOfSubview = [_sleepTimeDatePicker.subviews[0] subviews];
  498. NSUInteger subviewCount = subviewsOfSubview.count;
  499. for (NSUInteger x = 0; x < subviewCount; x++) {
  500. if ([subviewsOfSubview[x] isKindOfClass:[UILabel class]])
  501. [subviewsOfSubview[x] setTextColor:[UIColor VLCLightTextColor]];
  502. }
  503. }
  504. _sleepTimeDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
  505. _sleepTimeDatePicker.minuteInterval = 1;
  506. _sleepTimeDatePicker.minimumDate = [NSDate date];
  507. _sleepTimeDatePicker.countDownDuration = 1200.;
  508. [self.view addSubview:_sleepTimerContainer];
  509. }
  510. #pragma mark - controls visibility
  511. - (NSArray *)itemsForInterfaceLock
  512. {
  513. NSMutableArray *items = [[NSMutableArray alloc] initWithArray: @[_pinchRecognizer,
  514. _panRecognizer,
  515. _tapRecognizer,
  516. _doneButton,
  517. _timeNavigationTitleView.minimizePlaybackButton,
  518. _timeNavigationTitleView.positionSlider,
  519. _timeNavigationTitleView.aspectRatioButton,
  520. _controllerPanel.playbackSpeedButton,
  521. _controllerPanel.trackSwitcherButton,
  522. _controllerPanel.bwdButton,
  523. _controllerPanel.playPauseButton,
  524. _controllerPanel.fwdButton,
  525. _controllerPanel.videoFilterButton,
  526. _multiSelectionView.equalizerButton,
  527. _multiSelectionView.chapterSelectorButton,
  528. _multiSelectionView.repeatButton,
  529. _multiSelectionView.shuffleButton,
  530. _controllerPanel.volumeView]];
  531. [[UIDevice currentDevice] isiPhoneX] ? [items addObject:_tapToToggleiPhoneXRatioRecognizer]
  532. : [items addObject:_tapToSeekRecognizer];
  533. return [items copy];
  534. }
  535. - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
  536. {
  537. CGFloat diff = DEFAULT_FOV * -(ZOOM_SENSITIVITY * recognizer.velocity / _screenPixelSize.width);
  538. if ([_vpc currentMediaIs360Video]) {
  539. [self zoom360Video:diff];
  540. } else if (recognizer.velocity < 0. && _closeGestureEnabled) {
  541. [self minimizePlayback:nil];
  542. }
  543. }
  544. - (void)zoom360Video:(CGFloat)zoom
  545. {
  546. if ([_vpc updateViewpoint:0 pitch:0 roll:0 fov:zoom absolute:NO]) {
  547. //clamp Fov between min and max fov
  548. _fov = MAX(MIN(_fov + zoom, MAX_FOV), MIN_FOV);
  549. }
  550. }
  551. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  552. {
  553. if (touch.view != self.view)
  554. return NO;
  555. return YES;
  556. }
  557. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  558. {
  559. return YES;
  560. }
  561. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated
  562. {
  563. _controlsHidden = hidden;
  564. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  565. if (!_controlsHidden) {
  566. self.navigationController.navigationBar.alpha = 0.0;
  567. self.navigationController.navigationBar.hidden = NO;
  568. _controllerPanel.alpha = 0.0f;
  569. _controllerPanel.hidden = !_videoFiltersHidden;
  570. _videoFilterView.alpha = 0.0f;
  571. _videoFilterView.hidden = _videoFiltersHidden;
  572. _playbackSpeedView.alpha = 0.0f;
  573. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  574. _trackSelectorContainer.alpha = 0.0f;
  575. _trackSelectorContainer.hidden = YES;
  576. _equalizerView.alpha = 0.0f;
  577. _equalizerView.hidden = YES;
  578. if (_sleepTimerContainer) {
  579. _sleepTimerContainer.alpha = 0.0f;
  580. _sleepTimerContainer.hidden = YES;
  581. }
  582. _multiSelectionView.alpha = 0.0f;
  583. _multiSelectionView.hidden = YES;
  584. _artistNameLabel.hidden = NO;
  585. _albumNameLabel.hidden = NO;
  586. _trackNameLabel.hidden = NO;
  587. }
  588. void (^animationBlock)() = ^() {
  589. self.navigationController.navigationBar.alpha = alpha;
  590. _controllerPanel.alpha = alpha;
  591. _videoFilterView.alpha = alpha;
  592. _playbackSpeedView.alpha = alpha;
  593. _trackSelectorContainer.alpha = alpha;
  594. _equalizerView.alpha = alpha;
  595. _multiSelectionView.alpha = alpha;
  596. if (_sleepTimerContainer)
  597. _sleepTimerContainer.alpha = alpha;
  598. CGFloat metaInfoAlpha = _audioOnly ? 1.0f : alpha;
  599. _artistNameLabel.alpha = metaInfoAlpha;
  600. _albumNameLabel.alpha = metaInfoAlpha;
  601. _trackNameLabel.alpha = metaInfoAlpha;
  602. };
  603. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  604. _controllerPanel.hidden = _videoFiltersHidden ? _controlsHidden : NO;
  605. _videoFilterView.hidden = _videoFiltersHidden;
  606. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  607. self.navigationController.navigationBar.hidden = _controlsHidden;
  608. _trackSelectorContainer.hidden = YES;
  609. _equalizerView.hidden = YES;
  610. if (_sleepTimerContainer)
  611. _sleepTimerContainer.hidden = YES;
  612. _multiSelectionView.hidden = YES;
  613. _artistNameLabel.hidden = _audioOnly ? NO : _controlsHidden;
  614. _albumNameLabel.hidden = _audioOnly ? NO : _controlsHidden;
  615. _trackNameLabel.hidden = _audioOnly ? NO : _controlsHidden;
  616. };
  617. UIStatusBarAnimation animationType = animated? UIStatusBarAnimationFade: UIStatusBarAnimationNone;
  618. NSTimeInterval animationDuration = animated? 0.3: 0.0;
  619. [[UIApplication sharedApplication] setStatusBarHidden:_viewAppeared ? _controlsHidden : NO withAnimation:animationType];
  620. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  621. }
  622. - (void)toggleControlsVisible
  623. {
  624. if (!_trackSelectorContainer.hidden) {
  625. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  626. [recognizer setEnabled:YES];
  627. }
  628. if (_controlsHidden && !_videoFiltersHidden)
  629. _videoFiltersHidden = YES;
  630. if (_isTapSeeking)
  631. _numberOfTapSeek = 0;
  632. [self setControlsHidden:!_controlsHidden animated:YES];
  633. }
  634. - (void)_resetIdleTimer
  635. {
  636. if (!_idleTimer)
  637. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:4.
  638. target:self
  639. selector:@selector(idleTimerExceeded)
  640. userInfo:nil
  641. repeats:NO];
  642. else {
  643. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 4.)
  644. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:4.]];
  645. }
  646. }
  647. - (NSString *)_stringInTimeFormatFrom:(int)duration
  648. {
  649. if (duration < 60) {
  650. return [NSString stringWithFormat:@"%is", duration];
  651. } else {
  652. return [NSString stringWithFormat:@"%im%is", duration / 60, duration % 60];
  653. }
  654. }
  655. - (void)_seekFromTap
  656. {
  657. NSMutableString *hudString = [NSMutableString string];
  658. int seekDuration = (int)_numberOfTapSeek * SHORT_JUMP_DURATION;
  659. if (seekDuration > 0) {
  660. [_vpc jumpForward:SHORT_JUMP_DURATION];
  661. [hudString appendString:@"⇒ "];
  662. _previousJumpState = VLCMovieJumpStateForward;
  663. } else {
  664. [_vpc jumpBackward:SHORT_JUMP_DURATION];
  665. [hudString appendString:@"⇐ "];
  666. _previousJumpState = VLCMovieJumpStateBackward;
  667. }
  668. [hudString appendString: [self _stringInTimeFormatFrom:abs(seekDuration)]];
  669. [self.statusLabel showStatusMessage:[NSString stringWithString:hudString]];
  670. if (_controlsHidden)
  671. [self setControlsHidden:NO animated:NO];
  672. }
  673. - (void)idleTimerExceeded
  674. {
  675. if (![NSThread isMainThread]) {
  676. [self performSelectorOnMainThread:@selector(idleTimerExceeded) withObject:nil waitUntilDone:NO];
  677. return;
  678. }
  679. _idleTimer = nil;
  680. if (!_controlsHidden)
  681. [self toggleControlsVisible];
  682. if (_isTapSeeking) {
  683. _isTapSeeking = NO;
  684. _numberOfTapSeek = 0;
  685. }
  686. if (!_videoFiltersHidden)
  687. _videoFiltersHidden = YES;
  688. if (_equalizerView.hidden == NO)
  689. _equalizerView.hidden = YES;
  690. if (!_playbackSpeedViewHidden)
  691. _playbackSpeedViewHidden = YES;
  692. if (self.scrubIndicatorView.hidden == NO)
  693. self.scrubIndicatorView.hidden = YES;
  694. }
  695. - (UIResponder *)nextResponder
  696. {
  697. [self _resetIdleTimer];
  698. return [super nextResponder];
  699. }
  700. - (VLCDeviceMotion *)deviceMotion
  701. {
  702. if (!_deviceMotion) {
  703. _deviceMotion = [VLCDeviceMotion new];
  704. _deviceMotion.delegate = self;
  705. }
  706. return _deviceMotion;
  707. }
  708. - (void)setupForMediaProjection
  709. {
  710. BOOL mediaHasProjection = [_vpc currentMediaIs360Video];
  711. _fov = mediaHasProjection ? DEFAULT_FOV : 0.f;
  712. [_panRecognizer setEnabled:mediaHasProjection];
  713. [_swipeRecognizerUp setEnabled:!mediaHasProjection];
  714. [_swipeRecognizerDown setEnabled:!mediaHasProjection];
  715. [_swipeRecognizerLeft setEnabled:!mediaHasProjection];
  716. [_swipeRecognizerRight setEnabled:!mediaHasProjection];
  717. if (mediaHasProjection) {
  718. [self.deviceMotion startDeviceMotion];
  719. }
  720. }
  721. - (void)deviceMotionHasAttitudeWithDeviceMotion:(VLCDeviceMotion *)deviceMotion pitch:(double)pitch yaw:(double)yaw roll:(double)roll
  722. {
  723. if (_panRecognizer.state != UIGestureRecognizerStateChanged || UIGestureRecognizerStateBegan) {
  724. [_vpc updateViewpoint:yaw pitch:pitch roll:roll fov:_fov absolute:YES];
  725. }
  726. }
  727. #pragma mark - controls
  728. - (IBAction)closePlayback:(id)sender
  729. {
  730. _playbackWillClose = YES;
  731. [_vpc stopPlayback];
  732. }
  733. - (IBAction)minimizePlayback:(id)sender
  734. {
  735. [_delegate movieViewControllerDidSelectMinimize:self];
  736. }
  737. - (IBAction)positionSliderAction:(UISlider *)sender
  738. {
  739. /* we need to limit the number of events sent by the slider, since otherwise, the user
  740. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  741. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  742. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  743. if (_mediaDuration > 0) {
  744. VLCTime *newPosition = [VLCTime timeWithInt:(int)(sender.value * _mediaDuration)];
  745. [self.timeNavigationTitleView.timeDisplayButton setTitle:newPosition.stringValue forState:UIControlStateNormal];
  746. [self.timeNavigationTitleView setNeedsLayout];
  747. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"PLAYBACK_POSITION", nil), newPosition.stringValue];
  748. _positionSet = NO;
  749. }
  750. [self _resetIdleTimer];
  751. }
  752. - (void)_setPositionForReal
  753. {
  754. if (!_positionSet) {
  755. [_vpc setPlaybackPosition:self.timeNavigationTitleView.positionSlider.value];
  756. [_vpc setNeedsMetadataUpdate];
  757. _positionSet = YES;
  758. }
  759. }
  760. - (IBAction)positionSliderTouchDown:(id)sender
  761. {
  762. [self _updateScrubLabel];
  763. self.scrubIndicatorView.hidden = NO;
  764. _isScrubbing = YES;
  765. }
  766. - (IBAction)positionSliderTouchUp:(id)sender
  767. {
  768. self.scrubIndicatorView.hidden = YES;
  769. _isScrubbing = NO;
  770. }
  771. - (void)_updateScrubLabel
  772. {
  773. float speed = self.timeNavigationTitleView.positionSlider.scrubbingSpeed;
  774. if (speed == 1.)
  775. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", nil);
  776. else if (speed == .5)
  777. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", nil);
  778. else if (speed == .25)
  779. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", nil);
  780. else
  781. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", nil);
  782. [self _resetIdleTimer];
  783. }
  784. - (IBAction)positionSliderDrag:(id)sender
  785. {
  786. [self _updateScrubLabel];
  787. }
  788. - (void)volumeSliderAction:(id)sender
  789. {
  790. [self _resetIdleTimer];
  791. }
  792. - (void)updateTimeDisplayButton
  793. {
  794. UIButton *timeDisplayButton = self.timeNavigationTitleView.timeDisplayButton;
  795. if (_displayRemainingTime)
  796. [timeDisplayButton setTitle:[[_vpc remainingTime] stringValue] forState:UIControlStateNormal];
  797. else
  798. [timeDisplayButton setTitle:[[_vpc playedTime] stringValue] forState:UIControlStateNormal];
  799. [self.timeNavigationTitleView setNeedsLayout];
  800. }
  801. - (void)updateSleepTimerButton
  802. {
  803. NSMutableString *title = [NSMutableString stringWithString:NSLocalizedString(@"BUTTON_SLEEP_TIMER", nil)];
  804. if (_vpc.sleepTimer != nil && _vpc.sleepTimer.valid) {
  805. int remainSeconds = (int)[_vpc.sleepTimer.fireDate timeIntervalSinceNow];
  806. int hour = remainSeconds / 3600;
  807. int minute = (remainSeconds - hour * 3600) / 60;
  808. int second = remainSeconds % 60;
  809. [title appendFormat:@" %02d:%02d:%02d", hour, minute, second];
  810. } else {
  811. [_sleepCountDownTimer invalidate];
  812. }
  813. [_sleepTimerButton setTitle:title forState:UIControlStateNormal];
  814. }
  815. #pragma mark - playback controller delegation
  816. - (void)playbackPositionUpdated:(VLCPlaybackController *)controller
  817. {
  818. if (!_isScrubbing) {
  819. self.timeNavigationTitleView.positionSlider.value = [controller playbackPosition];
  820. }
  821. [self updateTimeDisplayButton];
  822. }
  823. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  824. {
  825. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  826. self.timeNavigationTitleView.positionSlider.value = 0.;
  827. [self.timeNavigationTitleView.timeDisplayButton setTitle:@"" forState:UIControlStateNormal];
  828. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = @"";
  829. [_equalizerView reloadData];
  830. double playbackRate = controller.playbackRate;
  831. self.playbackSpeedSlider.value = log2(playbackRate);
  832. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", playbackRate];
  833. float audioDelay = controller.audioDelay;
  834. self.audioDelaySlider.value = audioDelay;
  835. self.audioDelayIndicator.text = [NSString stringWithFormat:@"%d ms", (int) audioDelay];
  836. float subtitleDelay = controller.subtitleDelay;
  837. self.spuDelaySlider.value = subtitleDelay;
  838. self.spuDelayIndicator.text = [NSString stringWithFormat:@"%d ms", (int) subtitleDelay];
  839. [self _resetIdleTimer];
  840. }
  841. - (void)playbackDidStop:(NSNotification *)notification
  842. {
  843. [self minimizePlayback:nil];
  844. }
  845. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  846. isPlaying:(BOOL)isPlaying
  847. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  848. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  849. forPlaybackController:(VLCPlaybackController *)controller
  850. {
  851. if (currentState == VLCMediaPlayerStateBuffering)
  852. _mediaDuration = controller.mediaDuration;
  853. if (currentState == VLCMediaPlayerStateError)
  854. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", nil)];
  855. [_controllerPanel updateButtons];
  856. _multiSelectionView.mediaHasChapters = currentMediaHasChapters;
  857. }
  858. - (void)showStatusMessage:(NSString *)statusMessage forPlaybackController:(VLCPlaybackController *)controller
  859. {
  860. [self.statusLabel showStatusMessage:statusMessage];
  861. }
  862. - (void)hideShowAspectratioButton:(BOOL)hide
  863. {
  864. [UIView animateWithDuration:.3
  865. animations:^{
  866. self.widthConstraint.constant = hide ? 0 : 30;
  867. self.timeNavigationTitleView.aspectRatioButton.hidden = hide;
  868. }];
  869. }
  870. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller metadata:(VLCMetaData *)metadata
  871. {
  872. if (!_viewAppeared)
  873. return;
  874. self.trackNameLabel.text = metadata.title;
  875. self.artworkImageView.image = metadata.artworkImage;
  876. if (!metadata.artworkImage) {
  877. self.artistNameLabel.text = metadata.artist;
  878. self.albumNameLabel.text = metadata.albumName;
  879. } else
  880. self.artistNameLabel.text = self.albumNameLabel.text = nil;
  881. [self hideShowAspectratioButton:metadata.isAudioOnly];
  882. [_controllerPanel updateButtons];
  883. _audioOnly = metadata.isAudioOnly;
  884. }
  885. - (IBAction)playPause
  886. {
  887. [_vpc playPause];
  888. }
  889. - (IBAction)forward:(id)sender
  890. {
  891. [_vpc next];
  892. }
  893. - (IBAction)backward:(id)sender
  894. {
  895. [_vpc previous];
  896. }
  897. - (IBAction)switchTrack:(id)sender
  898. {
  899. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == NO) {
  900. _trackSelectorContainer.switchingTracksNotChapters = YES;
  901. _trackSelectorContainer.hidden = NO;
  902. _trackSelectorContainer.alpha = 1.;
  903. [_trackSelectorContainer updateView];
  904. if (_equalizerView.hidden == NO)
  905. _equalizerView.hidden = YES;
  906. if (!_playbackSpeedViewHidden)
  907. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  908. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  909. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  910. [recognizer setEnabled:NO];
  911. [_tapOnVideoRecognizer setEnabled:YES];
  912. } else {
  913. _trackSelectorContainer.hidden = YES;
  914. _trackSelectorContainer.switchingTracksNotChapters = NO;
  915. }
  916. }
  917. - (IBAction)toggleTimeDisplay:(id)sender
  918. {
  919. _displayRemainingTime = !_displayRemainingTime;
  920. [self updateTimeDisplayButton];
  921. [self _resetIdleTimer];
  922. }
  923. - (IBAction)sleepTimer:(id)sender
  924. {
  925. if (!_playbackSpeedViewHidden)
  926. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  927. if (_equalizerView.hidden == NO)
  928. _equalizerView.hidden = YES;
  929. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  930. if (!_controlsHidden) {
  931. self.controllerPanel.hidden = _controlsHidden = YES;
  932. }
  933. }
  934. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  935. _sleepTimerContainer.alpha = 1.;
  936. _sleepTimerContainer.hidden = NO;
  937. }
  938. - (IBAction)sleepTimerCancel:(id)sender
  939. {
  940. NSTimer *sleepTimer = [_vpc sleepTimer];
  941. if (sleepTimer) {
  942. [sleepTimer invalidate];
  943. sleepTimer = nil;
  944. }
  945. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  946. [self setControlsHidden:YES animated:YES];
  947. }
  948. - (IBAction)sleepTimerAction:(id)sender
  949. {
  950. [_vpc scheduleSleepTimerWithInterval:_sleepTimeDatePicker.countDownDuration];
  951. if (_sleepCountDownTimer == nil || _sleepCountDownTimer.valid == NO) {
  952. _sleepCountDownTimer = [NSTimer scheduledTimerWithTimeInterval:1
  953. target:self
  954. selector:@selector(updateSleepTimerButton)
  955. userInfo:nil
  956. repeats:YES];
  957. }
  958. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  959. [self setControlsHidden:YES animated:YES];
  960. }
  961. - (void)moreActions:(UIButton *)sender
  962. {
  963. if (_multiSelectionView.hidden == NO) {
  964. [UIView animateWithDuration:.3
  965. animations:^{
  966. _multiSelectionView.hidden = YES;
  967. }
  968. completion:^(BOOL finished){
  969. }];
  970. return;
  971. }
  972. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  973. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
  974. _multiSelectionView.showsEqualizer = YES;
  975. } else {
  976. _multiSelectionView.showsEqualizer = NO;
  977. }
  978. }
  979. CGRect workFrame = _multiSelectionView.frame;
  980. workFrame.size = [_multiSelectionView proposedDisplaySize];
  981. workFrame.origin.x = CGRectGetMaxX(sender.frame) - workFrame.size.width;
  982. _multiSelectionView.alpha = 1.0f;
  983. /* animate */
  984. _multiSelectionView.frame = CGRectMake(workFrame.origin.x, workFrame.origin.y + workFrame.size.height, workFrame.size.width, 0.);
  985. [UIView animateWithDuration:.3
  986. animations:^{
  987. _multiSelectionView.frame = workFrame;
  988. _multiSelectionView.hidden = NO;
  989. }
  990. completion:^(BOOL finished){
  991. }];
  992. [self _resetIdleTimer];
  993. }
  994. #pragma mark - multi-select delegation
  995. - (void)toggleUILock
  996. {
  997. _interfaceIsLocked = !_interfaceIsLocked;
  998. NSArray *items = [self itemsForInterfaceLock];
  999. for (NSObject *item in items) {
  1000. if ([item isKindOfClass:[UIControl class]]) {
  1001. UIControl *control = (UIControl *)item;
  1002. control.enabled = !_interfaceIsLocked;
  1003. } else if ([item isKindOfClass:[UIGestureRecognizer class]]){
  1004. UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)item;
  1005. gestureRecognizer.enabled = !_interfaceIsLocked;
  1006. } else if ([item isKindOfClass:[VLCVolumeView class]]) {
  1007. //The MPVolumeview doesn't adjust it's UI when disabled so we need to set the alpha by hand
  1008. VLCVolumeView *view = (VLCVolumeView *)item;
  1009. view.userInteractionEnabled = !_interfaceIsLocked;
  1010. view.alpha = _interfaceIsLocked ? 0.5 : 1;
  1011. } else {
  1012. NSAssert(NO, @"class not handled");
  1013. }
  1014. }
  1015. _multiSelectionView.displayLock = _interfaceIsLocked;
  1016. }
  1017. - (void)toggleEqualizer
  1018. {
  1019. if (_equalizerView.hidden) {
  1020. if (!_playbackSpeedViewHidden)
  1021. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1022. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1023. if (!_controlsHidden) {
  1024. self.controllerPanel.hidden = _controlsHidden = YES;
  1025. self.navigationController.navigationBar.hidden = YES;
  1026. }
  1027. }
  1028. _trackSelectorContainer.hidden = YES;
  1029. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1030. _equalizerView.alpha = 1.;
  1031. _equalizerView.hidden = NO;
  1032. } else
  1033. _equalizerView.hidden = YES;
  1034. }
  1035. - (void)toggleChapterAndTitleSelector
  1036. {
  1037. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == YES) {
  1038. _trackSelectorContainer.switchingTracksNotChapters = NO;
  1039. [_trackSelectorContainer updateView];
  1040. _trackSelectorContainer.hidden = NO;
  1041. _trackSelectorContainer.alpha = 1.;
  1042. if (_equalizerView.hidden == NO)
  1043. _equalizerView.hidden = YES;
  1044. if (!_playbackSpeedViewHidden)
  1045. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1046. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1047. if (!_controlsHidden) {
  1048. self.controllerPanel.hidden = _controlsHidden = YES;
  1049. }
  1050. }
  1051. _sleepTimerContainer.hidden = YES;
  1052. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1053. } else {
  1054. _trackSelectorContainer.hidden = YES;
  1055. }
  1056. }
  1057. - (void)toggleRepeatMode
  1058. {
  1059. [[VLCPlaybackController sharedInstance] toggleRepeatMode];
  1060. _multiSelectionView.repeatMode = [VLCPlaybackController sharedInstance].repeatMode;
  1061. }
  1062. - (void)toggleShuffleMode
  1063. {
  1064. _vpc.shuffleMode = !_vpc.isShuffleMode;
  1065. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  1066. }
  1067. - (void)hideMenu
  1068. {
  1069. [UIView animateWithDuration:.2
  1070. animations:^{
  1071. _multiSelectionView.hidden = YES;
  1072. }
  1073. completion:^(BOOL finished){
  1074. }];
  1075. [self _resetIdleTimer];
  1076. }
  1077. #pragma mark - multi-touch gestures
  1078. - (void)togglePlayPause
  1079. {
  1080. if (!_playPauseGestureEnabled)
  1081. return;
  1082. if (_vpc.isPlaying) {
  1083. [_vpc pause];
  1084. [self setControlsHidden:NO animated:_controlsHidden];
  1085. } else {
  1086. [_vpc play];
  1087. }
  1088. }
  1089. - (void)toggleiPhoneXAspectRatio
  1090. {
  1091. if (@available(iOS 11.0, *)) {
  1092. BOOL isFullScreen = CGRectEqualToRect(_movieView.frame , self.view.frame);
  1093. CGRect frameWithoutNotch = self.view.safeAreaLayoutGuide.layoutFrame;
  1094. [UIView animateWithDuration:0.3 animations:^{
  1095. _movieView.frame = isFullScreen ? frameWithoutNotch : self.view.frame;
  1096. }];
  1097. }
  1098. }
  1099. - (BOOL)prefersHomeIndicatorAutoHidden
  1100. {
  1101. return YES;
  1102. }
  1103. - (VLCPanType)detectPanTypeForPan:(UIPanGestureRecognizer*)panRecognizer
  1104. {
  1105. NSString *deviceType = [[UIDevice currentDevice] model];
  1106. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  1107. CGFloat windowWidth = CGRectGetWidth(window.bounds);
  1108. CGPoint location = [panRecognizer locationInView:window];
  1109. VLCPanType panType = VLCPanTypeVolume; // default or right side of the screen
  1110. if (location.x < windowWidth / 2)
  1111. panType = VLCPanTypeBrightness;
  1112. // only check for seeking gesture if on iPad , will overwrite last statements if true
  1113. if ([deviceType isEqualToString:@"iPad"] && location.y < 110) {
  1114. panType = VLCPanTypeSeek;
  1115. }
  1116. if ([_vpc currentMediaIs360Video]) {
  1117. panType = VLCPanTypeProjection;
  1118. }
  1119. return panType;
  1120. }
  1121. - (void)panRecognized:(UIPanGestureRecognizer*)panRecognizer
  1122. {
  1123. CGFloat panDirectionX = [panRecognizer velocityInView:self.view].x;
  1124. CGFloat panDirectionY = [panRecognizer velocityInView:self.view].y;
  1125. if (panRecognizer.state == UIGestureRecognizerStateBegan) {
  1126. _currentPanType = [self detectPanTypeForPan:panRecognizer];
  1127. if ([_vpc currentMediaIs360Video]) {
  1128. _saveLocation = [panRecognizer locationInView:self.view];
  1129. [_deviceMotion stopDeviceMotion];
  1130. }
  1131. }
  1132. switch (_currentPanType) {
  1133. case VLCPanTypeSeek: {
  1134. if (!_seekGestureEnabled)
  1135. return;
  1136. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1137. int timeRemaining = timeRemainingDouble;
  1138. if (panDirectionX > 0) {
  1139. if (timeRemaining > 2 ) // to not go outside duration , video will stop
  1140. [_vpc jumpForward:1];
  1141. } else
  1142. [_vpc jumpBackward:1];
  1143. break;
  1144. case VLCPanTypeVolume:
  1145. if (!_volumeGestureEnabled)
  1146. return;
  1147. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
  1148. #pragma clang diagnostic push
  1149. #pragma clang diagnostic ignored "-Wdeprecated"
  1150. // there is no replacement for .volume which we want to use since Apple's susggestion is to not use their overlays
  1151. // but switch to the volume slider exclusively. meh.
  1152. if (panDirectionY > 0)
  1153. musicPlayer.volume -= 0.01;
  1154. else
  1155. musicPlayer.volume += 0.01;
  1156. #pragma clang diagnostic pop
  1157. } break;
  1158. case VLCPanTypeBrightness: {
  1159. if (!_brightnessGestureEnabled)
  1160. return;
  1161. CGFloat brightness = [UIScreen mainScreen].brightness;
  1162. if (panDirectionY > 0)
  1163. brightness = brightness - 0.01;
  1164. else
  1165. brightness = brightness + 0.01;
  1166. // Sanity check since -[UIScreen brightness] does not go by 0.01 steps
  1167. if (brightness > 1.0)
  1168. brightness = 1.0;
  1169. else if (brightness < 0.0)
  1170. brightness = 0.0;
  1171. NSAssert(brightness >= 0 && brightness <= 1, @"Brightness must be within 0 and 1 (it is %f)", brightness);
  1172. [[UIScreen mainScreen] setBrightness:brightness];
  1173. NSString *brightnessHUD = [NSString stringWithFormat:@"%@: %@ %%", NSLocalizedString(@"VFILTER_BRIGHTNESS", nil), [[[NSString stringWithFormat:@"%f",(brightness*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  1174. [self.statusLabel showStatusMessage:brightnessHUD];
  1175. } break;
  1176. case VLCPanTypeProjection: {
  1177. [self updateProjectionWithPanRecognizer:panRecognizer];
  1178. } break;
  1179. case VLCPanTypeNone: {
  1180. } break;
  1181. }
  1182. if (panRecognizer.state == UIGestureRecognizerStateEnded) {
  1183. _currentPanType = VLCPanTypeNone;
  1184. if ([_vpc currentMediaIs360Video]) {
  1185. [_deviceMotion lastAngleWithYaw:_vpc.yaw pitch:_vpc.pitch roll:_vpc.roll];
  1186. [_deviceMotion startDeviceMotion];
  1187. }
  1188. }
  1189. }
  1190. - (void)updateProjectionWithPanRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer
  1191. {
  1192. CGPoint newLocationInView = [panGestureRecognizer locationInView:self.view];
  1193. CGFloat diffX = newLocationInView.x - _saveLocation.x;
  1194. CGFloat diffY = newLocationInView.y - _saveLocation.y;
  1195. _saveLocation = newLocationInView;
  1196. //screenSizePixel width is used twice to get a constant speed on the movement.
  1197. CGFloat yaw = _fov * -diffX / _screenPixelSize.width;
  1198. CGFloat pitch = _fov * -diffY / _screenPixelSize.width;
  1199. CGFloat newYaw = _vpc.yaw + yaw;
  1200. CGFloat newPitch = _vpc.pitch + pitch;
  1201. [_vpc updateViewpoint:newYaw pitch:newPitch roll:_vpc.roll fov:_vpc.fov absolute:YES];
  1202. }
  1203. - (void)swipeRecognized:(UISwipeGestureRecognizer*)swipeRecognizer
  1204. {
  1205. if (!_seekGestureEnabled)
  1206. return;
  1207. NSString * hudString = @" ";
  1208. int swipeForwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : FORWARD_SWIPE_DURATION;
  1209. int swipeBackwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : BACKWARD_SWIPE_DURATION;
  1210. if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
  1211. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1212. int timeRemaining = timeRemainingDouble;
  1213. if (swipeForwardDuration < timeRemaining) {
  1214. if (swipeForwardDuration < 1)
  1215. swipeForwardDuration = 1;
  1216. [_vpc jumpForward:swipeForwardDuration];
  1217. hudString = [NSString stringWithFormat:@"⇒ %is", swipeForwardDuration];
  1218. } else {
  1219. [_vpc jumpForward:(timeRemaining - 5)];
  1220. hudString = [NSString stringWithFormat:@"⇒ %is",(timeRemaining - 5)];
  1221. }
  1222. }
  1223. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
  1224. [_vpc jumpBackward:swipeBackwardDuration];
  1225. hudString = [NSString stringWithFormat:@"⇐ %is",swipeBackwardDuration];
  1226. }else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionUp) {
  1227. [self backward:self];
  1228. }
  1229. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionDown) {
  1230. [self forward:self];
  1231. }
  1232. if (swipeRecognizer.state == UIGestureRecognizerStateEnded) {
  1233. [self.statusLabel showStatusMessage:hudString];
  1234. }
  1235. }
  1236. - (void)tapToSeekRecognized:(UITapGestureRecognizer *)tapRecognizer
  1237. {
  1238. if (!_seekGestureEnabled)
  1239. return;
  1240. CGFloat screenHalf;
  1241. CGFloat tmpPosition;
  1242. CGSize size = self.view.frame.size;
  1243. CGPoint tapPosition = [tapRecognizer locationInView:self.view];
  1244. screenHalf = size.width / 2;
  1245. tmpPosition = tapPosition.x;
  1246. //Handling seek reset if tap orientation changes.
  1247. if (tmpPosition < screenHalf) {
  1248. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateForward ? -1 : _numberOfTapSeek - 1;
  1249. } else {
  1250. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateBackward ? 1 : _numberOfTapSeek + 1;
  1251. }
  1252. _isTapSeeking = YES;
  1253. [self _seekFromTap];
  1254. }
  1255. - (void)equalizerViewReceivedUserInput
  1256. {
  1257. [self _resetIdleTimer];
  1258. }
  1259. #pragma mark - Video Filter UI
  1260. - (IBAction)videoFilterToggle:(id)sender
  1261. {
  1262. if (!_playbackSpeedViewHidden)
  1263. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1264. if (_equalizerView.hidden == NO)
  1265. _equalizerView.hidden = YES;
  1266. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1267. if (!_controlsHidden) {
  1268. self.controllerPanel.hidden = _controlsHidden = YES;
  1269. }
  1270. }
  1271. self.videoFilterView.hidden = !_videoFiltersHidden;
  1272. _videoFiltersHidden = self.videoFilterView.hidden;
  1273. }
  1274. - (IBAction)videoFilterSliderAction:(id)sender
  1275. {
  1276. if (sender == self.hueSlider)
  1277. _vpc.hue = self.hueSlider.value;
  1278. else if (sender == self.contrastSlider)
  1279. _vpc.contrast = self.contrastSlider.value;
  1280. else if (sender == self.brightnessSlider)
  1281. _vpc.brightness = self.brightnessSlider.value;
  1282. else if (sender == self.saturationSlider)
  1283. _vpc.saturation = self.saturationSlider.value;
  1284. else if (sender == self.gammaSlider)
  1285. _vpc.gamma = self.gammaSlider.value;
  1286. else if (sender == self.resetVideoFilterButton) {
  1287. [self resetVideoFiltersSliders];
  1288. [_vpc resetFilters];
  1289. } else
  1290. APLog(@"unknown sender for videoFilterSliderAction");
  1291. [self _resetIdleTimer];
  1292. }
  1293. - (void)appBecameActive:(NSNotification *)aNotification
  1294. {
  1295. if ([_delegate movieViewControllerShouldBeDisplayed:self]) {
  1296. [_vpc recoverDisplayedMetadata];
  1297. if (_vpc.videoOutputView != self.movieView) {
  1298. _vpc.videoOutputView = self.movieView;
  1299. }
  1300. }
  1301. }
  1302. #pragma mark - playback view
  1303. - (IBAction)playbackSliderAction:(UISlider *)sender
  1304. {
  1305. if (sender == _playbackSpeedSlider) {
  1306. double speed = exp2(sender.value);
  1307. _vpc.playbackRate = speed;
  1308. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  1309. } else if (sender == _audioDelaySlider) {
  1310. int delay = ((int) round(sender.value / 50.)) * 50;
  1311. _vpc.audioDelay = delay;
  1312. [sender setValue:delay animated:NO];
  1313. _audioDelayIndicator.text = [NSString stringWithFormat:@"%d ms", delay];
  1314. } else if (sender == _spuDelaySlider) {
  1315. int delay = (int) (round(sender.value / 50.)) * 50;
  1316. _vpc.subtitleDelay = delay;
  1317. [sender setValue:delay animated:NO];
  1318. _spuDelayIndicator.text = [NSString stringWithFormat:@"%d ms", delay];
  1319. }
  1320. [self _resetIdleTimer];
  1321. }
  1322. - (IBAction)videoDimensionAction:(id)sender
  1323. {
  1324. if (sender == self.timeNavigationTitleView.aspectRatioButton) {
  1325. [[VLCPlaybackController sharedInstance] switchAspectRatio];
  1326. }
  1327. }
  1328. - (IBAction)showPlaybackSpeedView {
  1329. if (!_videoFiltersHidden)
  1330. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1331. if (_equalizerView.hidden == NO)
  1332. _equalizerView.hidden = YES;
  1333. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  1334. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  1335. [self _resetIdleTimer];
  1336. }
  1337. #pragma mark - autorotation
  1338. - (BOOL)rotationIsDisabled
  1339. {
  1340. return _interfaceIsLocked || [_vpc currentMediaIs360Video];
  1341. }
  1342. - (BOOL)shouldAutorotate
  1343. {
  1344. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1345. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  1346. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  1347. }
  1348. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  1349. {
  1350. if (_vpc.isPlaying && _controlsHidden)
  1351. [self setControlsHidden:NO animated:YES];
  1352. }
  1353. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  1354. {
  1355. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1356. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1357. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1358. if (self.artworkImageView.image)
  1359. self.trackNameLabel.hidden = YES;
  1360. if (!_equalizerView.hidden)
  1361. _equalizerView.hidden = YES;
  1362. } completion:nil];
  1363. }
  1364. }
  1365. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
  1366. {
  1367. if (_vpc.isPlaying && _controlsHidden)
  1368. [self setControlsHidden:NO animated:YES];
  1369. }
  1370. #pragma mark - External Display
  1371. - (void)showOnExternalDisplay
  1372. {
  1373. UIScreen *screen = [UIScreen screens][1];
  1374. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  1375. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  1376. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  1377. self.externalWindow.rootViewController = controller;
  1378. [controller.view addSubview:_movieView];
  1379. controller.view.frame = screen.bounds;
  1380. _movieView.frame = screen.bounds;
  1381. self.playingExternallyView.hidden = NO;
  1382. self.externalWindow.screen = screen;
  1383. self.externalWindow.hidden = NO;
  1384. }
  1385. - (void)hideFromExternalDisplay
  1386. {
  1387. [self.view addSubview:_movieView];
  1388. [self.view sendSubviewToBack:_movieView];
  1389. _movieView.frame = self.view.frame;
  1390. self.playingExternallyView.hidden = YES;
  1391. self.externalWindow.hidden = YES;
  1392. self.externalWindow = nil;
  1393. }
  1394. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  1395. {
  1396. [self showOnExternalDisplay];
  1397. }
  1398. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  1399. {
  1400. [self hideFromExternalDisplay];
  1401. }
  1402. @end