VLCMovieViewController.m 65 KB

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