VLCMovieViewController.m 65 KB

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