VLCMovieViewController.m 64 KB

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