VLCMovieViewController.m 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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 - controls
  785. - (void)closePlayback:(id)sender
  786. {
  787. _playbackWillClose = YES;
  788. [_vpc stopPlayback];
  789. }
  790. - (IBAction)minimizePlayback:(id)sender
  791. {
  792. [_delegate movieViewControllerDidSelectMinimize:self];
  793. }
  794. - (IBAction)positionSliderAction:(UISlider *)sender
  795. {
  796. /* we need to limit the number of events sent by the slider, since otherwise, the user
  797. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  798. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  799. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  800. if (_mediaDuration > 0) {
  801. VLCTime *newPosition = [VLCTime timeWithInt:(int)(sender.value * _mediaDuration)];
  802. [self.timeNavigationTitleView.timeDisplayButton setTitle:newPosition.stringValue forState:UIControlStateNormal];
  803. [self.timeNavigationTitleView setNeedsLayout];
  804. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"PLAYBACK_POSITION", nil), newPosition.stringValue];
  805. _positionSet = NO;
  806. }
  807. [self _resetIdleTimer];
  808. }
  809. - (void)_setPositionForReal
  810. {
  811. if (!_positionSet) {
  812. [_vpc setPlaybackPosition:self.timeNavigationTitleView.positionSlider.value];
  813. [_vpc setNeedsMetadataUpdate];
  814. _positionSet = YES;
  815. }
  816. }
  817. - (IBAction)positionSliderTouchDown:(id)sender
  818. {
  819. [self _updateScrubLabel];
  820. self.scrubIndicatorView.hidden = NO;
  821. _isScrubbing = YES;
  822. }
  823. - (IBAction)positionSliderTouchUp:(id)sender
  824. {
  825. self.scrubIndicatorView.hidden = YES;
  826. _isScrubbing = NO;
  827. }
  828. - (void)_updateScrubLabel
  829. {
  830. float speed = self.timeNavigationTitleView.positionSlider.scrubbingSpeed;
  831. if (speed == 1.)
  832. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", nil);
  833. else if (speed == .5)
  834. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", nil);
  835. else if (speed == .25)
  836. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", nil);
  837. else
  838. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", nil);
  839. [self _resetIdleTimer];
  840. }
  841. - (IBAction)positionSliderDrag:(id)sender
  842. {
  843. [self _updateScrubLabel];
  844. }
  845. - (void)volumeSliderAction:(id)sender
  846. {
  847. [self _resetIdleTimer];
  848. }
  849. - (void)updateTimeDisplayButton
  850. {
  851. UIButton *timeDisplayButton = self.timeNavigationTitleView.timeDisplayButton;
  852. if (_displayRemainingTime)
  853. [timeDisplayButton setTitle:[[_vpc remainingTime] stringValue] forState:UIControlStateNormal];
  854. else
  855. [timeDisplayButton setTitle:[[_vpc playedTime] stringValue] forState:UIControlStateNormal];
  856. [self.timeNavigationTitleView setNeedsLayout];
  857. }
  858. #pragma mark - playback controller delegation
  859. - (void)playbackPositionUpdated:(VLCPlaybackController *)controller
  860. {
  861. // FIXME: hard coded state since the state in mediaPlayer is incorrectly still buffering
  862. [self updateActivityIndicatorForState:VLCMediaPlayerStatePlaying];
  863. if (!_isScrubbing) {
  864. self.timeNavigationTitleView.positionSlider.value = [controller playbackPosition];
  865. }
  866. [self updateTimeDisplayButton];
  867. }
  868. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  869. {
  870. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  871. self.timeNavigationTitleView.positionSlider.value = 0.;
  872. [self.timeNavigationTitleView.timeDisplayButton setTitle:@"" forState:UIControlStateNormal];
  873. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = @"";
  874. [_equalizerView reloadData];
  875. [_playbackSpeedView prepareForMediaPlaybackWithController:controller];
  876. [self _resetIdleTimer];
  877. }
  878. - (void)playbackDidStop:(NSNotification *)notification
  879. {
  880. [self minimizePlayback:nil];
  881. }
  882. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  883. isPlaying:(BOOL)isPlaying
  884. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  885. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  886. forPlaybackController:(VLCPlaybackController *)controller
  887. {
  888. [self updateActivityIndicatorForState:currentState];
  889. if (currentState == VLCMediaPlayerStateBuffering)
  890. _mediaDuration = controller.mediaDuration;
  891. if (currentState == VLCMediaPlayerStateError)
  892. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", nil)];
  893. [_controllerPanel updateButtons];
  894. _multiSelectionView.mediaHasChapters = currentMediaHasChapters;
  895. }
  896. - (void)showStatusMessage:(NSString *)statusMessage
  897. {
  898. [self.statusLabel showStatusMessage:statusMessage];
  899. }
  900. - (void)hideShowAspectratioButton:(BOOL)hide
  901. {
  902. [UIView animateWithDuration:.3
  903. animations:^{
  904. self.widthConstraint.constant = hide ? 0 : 30;
  905. self.timeNavigationTitleView.aspectRatioButton.hidden = hide;
  906. }];
  907. }
  908. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller metadata:(VLCMetaData *)metadata
  909. {
  910. if (!_viewAppeared)
  911. return;
  912. self.trackNameLabel.text = metadata.title;
  913. self.artworkImageView.image = metadata.artworkImage;
  914. if (!metadata.artworkImage) {
  915. self.artistNameLabel.text = metadata.artist;
  916. self.albumNameLabel.text = metadata.albumName;
  917. } else
  918. self.artistNameLabel.text = self.albumNameLabel.text = nil;
  919. [self hideShowAspectratioButton:metadata.isAudioOnly];
  920. [_controllerPanel updateButtons];
  921. [_playingExternalView updateUIWithRendererItem:_vpc.renderer title:metadata.title];
  922. _audioOnly = metadata.isAudioOnly;
  923. }
  924. - (IBAction)playPause
  925. {
  926. [_vpc playPause];
  927. }
  928. - (IBAction)forward:(id)sender
  929. {
  930. [_vpc next];
  931. }
  932. - (IBAction)backward:(id)sender
  933. {
  934. [_vpc previous];
  935. }
  936. - (IBAction)switchTrack:(id)sender
  937. {
  938. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == NO) {
  939. _trackSelectorContainer.switchingTracksNotChapters = YES;
  940. _trackSelectorContainer.hidden = NO;
  941. _trackSelectorContainer.alpha = 1.;
  942. [_trackSelectorContainer updateView];
  943. if (_equalizerView.hidden == NO)
  944. _equalizerView.hidden = YES;
  945. if (!_playbackSpeedViewHidden)
  946. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  947. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  948. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  949. [recognizer setEnabled:NO];
  950. [_tapOnVideoRecognizer setEnabled:YES];
  951. } else {
  952. _trackSelectorContainer.hidden = YES;
  953. _trackSelectorContainer.switchingTracksNotChapters = NO;
  954. }
  955. }
  956. - (IBAction)toggleTimeDisplay:(id)sender
  957. {
  958. _displayRemainingTime = !_displayRemainingTime;
  959. [self updateTimeDisplayButton];
  960. [self _resetIdleTimer];
  961. }
  962. - (void)showSleepTimer
  963. {
  964. if (!_playbackSpeedViewHidden)
  965. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  966. if (_equalizerView.hidden == NO)
  967. _equalizerView.hidden = YES;
  968. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  969. if (!_controlsHidden) {
  970. self.controllerPanel.hidden = _controlsHidden = YES;
  971. }
  972. }
  973. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  974. _sleepTimerContainer.alpha = 1.;
  975. _sleepTimerContainer.hidden = NO;
  976. }
  977. - (IBAction)sleepTimerCancel:(id)sender
  978. {
  979. NSTimer *sleepTimer = [_vpc sleepTimer];
  980. if (sleepTimer) {
  981. [sleepTimer invalidate];
  982. sleepTimer = nil;
  983. }
  984. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  985. [self setControlsHidden:YES animated:YES];
  986. }
  987. - (IBAction)sleepTimerAction:(id)sender
  988. {
  989. [_vpc scheduleSleepTimerWithInterval:_sleepTimeDatePicker.countDownDuration];
  990. [_playbackSpeedView setupSleepTimerIfNecessary];
  991. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  992. [self setControlsHidden:YES animated:YES];
  993. }
  994. - (void)moreActions:(UIButton *)sender
  995. {
  996. if (_multiSelectionView.hidden == NO) {
  997. [UIView animateWithDuration:.3
  998. animations:^{
  999. self->_multiSelectionView.hidden = YES;
  1000. }
  1001. completion:^(BOOL finished){
  1002. }];
  1003. return;
  1004. }
  1005. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1006. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
  1007. _multiSelectionView.showsEqualizer = YES;
  1008. } else {
  1009. _multiSelectionView.showsEqualizer = NO;
  1010. }
  1011. }
  1012. CGRect workFrame = _multiSelectionView.frame;
  1013. workFrame.size = [_multiSelectionView proposedDisplaySize];
  1014. workFrame.origin.x = CGRectGetMaxX(sender.frame) - workFrame.size.width;
  1015. _multiSelectionView.alpha = 1.0f;
  1016. /* animate */
  1017. _multiSelectionView.frame = CGRectMake(workFrame.origin.x, workFrame.origin.y + workFrame.size.height, workFrame.size.width, 0.);
  1018. [UIView animateWithDuration:.3
  1019. animations:^{
  1020. self->_multiSelectionView.frame = workFrame;
  1021. self->_multiSelectionView.hidden = NO;
  1022. }
  1023. completion:^(BOOL finished){
  1024. }];
  1025. [self _resetIdleTimer];
  1026. }
  1027. #pragma mark - multi-select delegation
  1028. - (void)toggleUILock
  1029. {
  1030. _interfaceIsLocked = !_interfaceIsLocked;
  1031. if (_interfaceIsLocked) {
  1032. _lockedOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1033. }
  1034. NSArray *items = [self itemsForInterfaceLock];
  1035. for (NSObject *item in items) {
  1036. if ([item isKindOfClass:[UIControl class]]) {
  1037. UIControl *control = (UIControl *)item;
  1038. control.enabled = !_interfaceIsLocked;
  1039. } else if ([item isKindOfClass:[UIGestureRecognizer class]]){
  1040. UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)item;
  1041. gestureRecognizer.enabled = !_interfaceIsLocked;
  1042. } else if ([item isKindOfClass:[VLCVolumeView class]]) {
  1043. //The MPVolumeview doesn't adjust it's UI when disabled so we need to set the alpha by hand
  1044. VLCVolumeView *view = (VLCVolumeView *)item;
  1045. view.userInteractionEnabled = !_interfaceIsLocked;
  1046. view.alpha = _interfaceIsLocked ? 0.5 : 1;
  1047. } else {
  1048. NSAssert(NO, @"class not handled");
  1049. }
  1050. }
  1051. _multiSelectionView.displayLock = _interfaceIsLocked;
  1052. }
  1053. - (void)toggleEqualizer
  1054. {
  1055. if (_equalizerView.hidden) {
  1056. if (!_playbackSpeedViewHidden)
  1057. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1058. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1059. if (!_controlsHidden) {
  1060. self.controllerPanel.hidden = _controlsHidden = YES;
  1061. self.navigationController.navigationBar.hidden = YES;
  1062. }
  1063. }
  1064. _trackSelectorContainer.hidden = YES;
  1065. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1066. _equalizerView.alpha = 1.;
  1067. _equalizerView.hidden = NO;
  1068. } else
  1069. _equalizerView.hidden = YES;
  1070. }
  1071. - (void)toggleChapterAndTitleSelector
  1072. {
  1073. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == YES) {
  1074. _trackSelectorContainer.switchingTracksNotChapters = NO;
  1075. [_trackSelectorContainer updateView];
  1076. _trackSelectorContainer.hidden = NO;
  1077. _trackSelectorContainer.alpha = 1.;
  1078. if (_equalizerView.hidden == NO)
  1079. _equalizerView.hidden = YES;
  1080. if (!_playbackSpeedViewHidden)
  1081. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1082. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1083. if (!_controlsHidden) {
  1084. self.controllerPanel.hidden = _controlsHidden = YES;
  1085. }
  1086. }
  1087. _sleepTimerContainer.hidden = YES;
  1088. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1089. } else {
  1090. _trackSelectorContainer.hidden = YES;
  1091. }
  1092. }
  1093. - (void)toggleRepeatMode
  1094. {
  1095. [[VLCPlaybackController sharedInstance] toggleRepeatMode];
  1096. _multiSelectionView.repeatMode = [VLCPlaybackController sharedInstance].repeatMode;
  1097. }
  1098. - (void)toggleShuffleMode
  1099. {
  1100. _vpc.shuffleMode = !_vpc.isShuffleMode;
  1101. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  1102. }
  1103. - (void)hideMenu
  1104. {
  1105. [UIView animateWithDuration:.2
  1106. animations:^{
  1107. self->_multiSelectionView.hidden = YES;
  1108. }
  1109. completion:^(BOOL finished){
  1110. }];
  1111. [self _resetIdleTimer];
  1112. }
  1113. #pragma mark - multi-touch gestures
  1114. - (void)togglePlayPause
  1115. {
  1116. if (!_playPauseGestureEnabled)
  1117. return;
  1118. if (_vpc.isPlaying) {
  1119. [_vpc pause];
  1120. [self setControlsHidden:NO animated:_controlsHidden];
  1121. } else {
  1122. [_vpc play];
  1123. }
  1124. }
  1125. - (BOOL)prefersHomeIndicatorAutoHidden
  1126. {
  1127. return YES;
  1128. }
  1129. - (VLCPanType)detectPanTypeForPan:(UIPanGestureRecognizer*)panRecognizer
  1130. {
  1131. NSString *deviceType = [[UIDevice currentDevice] model];
  1132. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  1133. CGFloat windowWidth = CGRectGetWidth(window.bounds);
  1134. CGPoint location = [panRecognizer locationInView:window];
  1135. VLCPanType panType = VLCPanTypeVolume; // default or right side of the screen
  1136. if (location.x < windowWidth / 2)
  1137. panType = VLCPanTypeBrightness;
  1138. // only check for seeking gesture if on iPad , will overwrite last statements if true
  1139. if ([deviceType isEqualToString:@"iPad"] && location.y < 110) {
  1140. panType = VLCPanTypeSeek;
  1141. }
  1142. if ([_vpc currentMediaIs360Video]) {
  1143. panType = VLCPanTypeProjection;
  1144. }
  1145. return panType;
  1146. }
  1147. - (void)panRecognized:(UIPanGestureRecognizer*)panRecognizer
  1148. {
  1149. CGFloat panDirectionX = [panRecognizer velocityInView:self.view].x;
  1150. CGFloat panDirectionY = [panRecognizer velocityInView:self.view].y;
  1151. if (panRecognizer.state == UIGestureRecognizerStateBegan) {
  1152. _currentPanType = [self detectPanTypeForPan:panRecognizer];
  1153. if ([_vpc currentMediaIs360Video]) {
  1154. _saveLocation = [panRecognizer locationInView:self.view];
  1155. [_deviceMotion stopDeviceMotion];
  1156. }
  1157. }
  1158. switch (_currentPanType) {
  1159. case VLCPanTypeSeek: {
  1160. if (!_seekGestureEnabled)
  1161. return;
  1162. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1163. int timeRemaining = timeRemainingDouble;
  1164. if (panDirectionX > 0) {
  1165. if (timeRemaining > 2 ) // to not go outside duration , video will stop
  1166. [_vpc jumpForward:1];
  1167. } else
  1168. [_vpc jumpBackward:1];
  1169. break;
  1170. case VLCPanTypeVolume:
  1171. if (!_volumeGestureEnabled)
  1172. return;
  1173. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
  1174. #pragma clang diagnostic push
  1175. #pragma clang diagnostic ignored "-Wdeprecated"
  1176. // there is no replacement for .volume which we want to use since Apple's susggestion is to not use their overlays
  1177. // but switch to the volume slider exclusively. meh.
  1178. if (panDirectionY > 0)
  1179. musicPlayer.volume -= 0.01;
  1180. else
  1181. musicPlayer.volume += 0.01;
  1182. #pragma clang diagnostic pop
  1183. } break;
  1184. case VLCPanTypeBrightness: {
  1185. if (!_brightnessGestureEnabled)
  1186. return;
  1187. CGFloat brightness = [UIScreen mainScreen].brightness;
  1188. if (panDirectionY > 0)
  1189. brightness = brightness - 0.01;
  1190. else
  1191. brightness = brightness + 0.01;
  1192. // Sanity check since -[UIScreen brightness] does not go by 0.01 steps
  1193. if (brightness > 1.0)
  1194. brightness = 1.0;
  1195. else if (brightness < 0.0)
  1196. brightness = 0.0;
  1197. NSAssert(brightness >= 0 && brightness <= 1, @"Brightness must be within 0 and 1 (it is %f)", brightness);
  1198. [[UIScreen mainScreen] setBrightness:brightness];
  1199. NSString *brightnessHUD = [NSString stringWithFormat:@"%@: %@ %%", NSLocalizedString(@"VFILTER_BRIGHTNESS", nil), [[[NSString stringWithFormat:@"%f",(brightness*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  1200. [self.statusLabel showStatusMessage:brightnessHUD];
  1201. } break;
  1202. case VLCPanTypeProjection: {
  1203. [self updateProjectionWithPanRecognizer:panRecognizer];
  1204. } break;
  1205. case VLCPanTypeNone: {
  1206. } break;
  1207. }
  1208. if (panRecognizer.state == UIGestureRecognizerStateEnded) {
  1209. _currentPanType = VLCPanTypeNone;
  1210. if ([_vpc currentMediaIs360Video]) {
  1211. [_deviceMotion startDeviceMotion];
  1212. }
  1213. }
  1214. }
  1215. - (void)updateProjectionWithPanRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer
  1216. {
  1217. CGPoint newLocationInView = [panGestureRecognizer locationInView:self.view];
  1218. CGFloat diffX = newLocationInView.x - _saveLocation.x;
  1219. CGFloat diffY = newLocationInView.y - _saveLocation.y;
  1220. _saveLocation = newLocationInView;
  1221. //screenSizePixel width is used twice to get a constant speed on the movement.
  1222. CGFloat diffYaw = _fov * -diffX / _screenPixelSize.width;
  1223. CGFloat diffPitch = _fov * -diffY / _screenPixelSize.width;
  1224. [self applyYaw:diffYaw pitch:diffPitch];
  1225. }
  1226. - (void)swipeRecognized:(UISwipeGestureRecognizer*)swipeRecognizer
  1227. {
  1228. if (!_seekGestureEnabled)
  1229. return;
  1230. NSString * hudString = @" ";
  1231. int swipeForwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : FORWARD_SWIPE_DURATION;
  1232. int swipeBackwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : BACKWARD_SWIPE_DURATION;
  1233. if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
  1234. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1235. int timeRemaining = timeRemainingDouble;
  1236. if (swipeForwardDuration < timeRemaining) {
  1237. if (swipeForwardDuration < 1)
  1238. swipeForwardDuration = 1;
  1239. [_vpc jumpForward:swipeForwardDuration];
  1240. hudString = [NSString stringWithFormat:@"⇒ %is", swipeForwardDuration];
  1241. } else {
  1242. [_vpc jumpForward:(timeRemaining - 5)];
  1243. hudString = [NSString stringWithFormat:@"⇒ %is",(timeRemaining - 5)];
  1244. }
  1245. }
  1246. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
  1247. [_vpc jumpBackward:swipeBackwardDuration];
  1248. hudString = [NSString stringWithFormat:@"⇐ %is",swipeBackwardDuration];
  1249. }else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionUp) {
  1250. [self backward:self];
  1251. }
  1252. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionDown) {
  1253. [self forward:self];
  1254. }
  1255. if (swipeRecognizer.state == UIGestureRecognizerStateEnded) {
  1256. [self.statusLabel showStatusMessage:hudString];
  1257. }
  1258. }
  1259. - (void)doubleTapRecognized:(UITapGestureRecognizer *)tapRecognizer
  1260. {
  1261. if (!_seekGestureEnabled)
  1262. return;
  1263. CGFloat screenWidth = self.view.frame.size.width;
  1264. CGFloat backwardBoundary = screenWidth / 3.0;
  1265. CGFloat forwardBoundary = 2 * screenWidth / 3.0;
  1266. CGPoint tapPosition = [tapRecognizer locationInView:self.view];
  1267. //Handling seek reset if tap orientation changes.
  1268. if (tapPosition.x < backwardBoundary) {
  1269. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateForward ? -1 : _numberOfTapSeek - 1;
  1270. } else if (tapPosition.x > forwardBoundary){
  1271. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateBackward ? 1 : _numberOfTapSeek + 1;
  1272. } else {
  1273. [_vpc toggleFullScreen];
  1274. return;
  1275. }
  1276. _isTapSeeking = YES;
  1277. [self _seekFromTap];
  1278. }
  1279. - (void)equalizerViewReceivedUserInput
  1280. {
  1281. [self _resetIdleTimer];
  1282. }
  1283. #pragma mark - Video Filter UI
  1284. - (IBAction)videoFilterToggle:(id)sender
  1285. {
  1286. if (!_playbackSpeedViewHidden)
  1287. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1288. if (_equalizerView.hidden == NO)
  1289. _equalizerView.hidden = YES;
  1290. self.videoFilterView.hidden = !_videoFiltersHidden;
  1291. _videoFiltersHidden = self.videoFilterView.hidden;
  1292. }
  1293. - (IBAction)videoFilterSliderAction:(id)sender
  1294. {
  1295. if (sender == self.hueSlider)
  1296. _vpc.hue = self.hueSlider.value;
  1297. else if (sender == self.contrastSlider)
  1298. _vpc.contrast = self.contrastSlider.value;
  1299. else if (sender == self.brightnessSlider)
  1300. _vpc.brightness = self.brightnessSlider.value;
  1301. else if (sender == self.saturationSlider)
  1302. _vpc.saturation = self.saturationSlider.value;
  1303. else if (sender == self.gammaSlider)
  1304. _vpc.gamma = self.gammaSlider.value;
  1305. else if (sender == self.resetVideoFilterButton) {
  1306. [self resetVideoFiltersSliders];
  1307. [_vpc resetFilters];
  1308. } else
  1309. APLog(@"unknown sender for videoFilterSliderAction");
  1310. [self _resetIdleTimer];
  1311. }
  1312. - (void)appBecameActive:(NSNotification *)aNotification
  1313. {
  1314. if ([_delegate movieViewControllerShouldBeDisplayed:self]) {
  1315. [_vpc recoverDisplayedMetadata];
  1316. if (_vpc.videoOutputView != self.movieView) {
  1317. _vpc.videoOutputView = self.movieView;
  1318. }
  1319. }
  1320. }
  1321. #pragma mark - playback view
  1322. - (IBAction)videoDimensionAction:(id)sender
  1323. {
  1324. if (sender == self.timeNavigationTitleView.aspectRatioButton) {
  1325. [[VLCPlaybackController sharedInstance] switchAspectRatio];
  1326. }
  1327. }
  1328. - (IBAction)showPlaybackSpeedView {
  1329. if (!_videoFiltersHidden)
  1330. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1331. if (_equalizerView.hidden == NO)
  1332. _equalizerView.hidden = YES;
  1333. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  1334. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  1335. [self _resetIdleTimer];
  1336. }
  1337. #pragma mark - autorotation
  1338. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  1339. {
  1340. BOOL orientationIslocked = _interfaceIsLocked || [_vpc currentMediaIs360Video];
  1341. UIInterfaceOrientationMask maskFromOrientation = 1 << _lockedOrientation;
  1342. return orientationIslocked ? maskFromOrientation : UIInterfaceOrientationMaskAll;
  1343. }
  1344. - (BOOL)shouldAutorotate
  1345. {
  1346. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1347. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  1348. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  1349. }
  1350. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  1351. {
  1352. if (_vpc.isPlaying && _controlsHidden)
  1353. [self setControlsHidden:NO animated:YES];
  1354. }
  1355. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  1356. {
  1357. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1358. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1359. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1360. if (self.artworkImageView.image)
  1361. self.trackNameLabel.hidden = YES;
  1362. if (!self->_equalizerView.hidden)
  1363. self->_equalizerView.hidden = YES;
  1364. } completion:nil];
  1365. }
  1366. }
  1367. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
  1368. {
  1369. if (_vpc.isPlaying && _controlsHidden)
  1370. [self setControlsHidden:NO animated:YES];
  1371. }
  1372. #pragma mark - External Display
  1373. - (void)showOnDisplay:(UIView *)view
  1374. {
  1375. // if we don't have a renderer we're mirroring and don't want to show the dialog
  1376. BOOL displayExternally = view != _movieView;
  1377. [_playingExternalView shouldDisplay:displayExternally movieView:_movieView];
  1378. _artworkImageView.hidden = displayExternally;
  1379. if (!displayExternally && _movieView.superview != self.view) {
  1380. [self.view addSubview:_movieView];
  1381. [self.view sendSubviewToBack:_movieView];
  1382. _movieView.frame = self.view.frame;
  1383. }
  1384. }
  1385. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  1386. {
  1387. [self showOnDisplay:_playingExternalView.displayView];
  1388. }
  1389. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  1390. {
  1391. [self showOnDisplay:_movieView];
  1392. }
  1393. #pragma mark - Renderers
  1394. - (void)setupRendererDiscovererManager
  1395. {
  1396. // Create a renderer button for VLCMovieViewController
  1397. _rendererButton = [_services.rendererDiscovererManager setupRendererButton];
  1398. // Setting rendererIcons to white since default is orange
  1399. _rendererButton.tintColor = [UIColor whiteColor];
  1400. [_rendererButton setImage:[UIImage imageNamed:@"renderer"] forState:UIControlStateNormal];
  1401. [_rendererButton setImage:[UIImage imageNamed:@"rendererFull"] forState:UIControlStateSelected];
  1402. __weak typeof(self) weakSelf = self;
  1403. [_services.rendererDiscovererManager addSelectionHandler:^(VLCRendererItem * item) {
  1404. __strong typeof(weakSelf) strongSelf = weakSelf;
  1405. if (item) {
  1406. [strongSelf showOnDisplay:strongSelf->_playingExternalView.displayView];
  1407. } else {
  1408. [strongSelf removedCurrentRendererItem:strongSelf->_vpc.renderer];
  1409. }
  1410. }];
  1411. }
  1412. #pragma mark - VLCRendererDiscovererManagerDelegate
  1413. - (void)removedCurrentRendererItem:(VLCRendererItem *)item
  1414. {
  1415. [self showOnDisplay:_movieView];
  1416. }
  1417. #pragma mark - PlaybackSpeedViewDelegate
  1418. - (void)playbackSpeedViewShouldResetIdleTimer:(PlaybackSpeedView *)playbackSpeedView
  1419. {
  1420. [self _resetIdleTimer];
  1421. }
  1422. - (void)playbackSpeedViewSleepTimerHit:(PlaybackSpeedView *)playbackSpeedView
  1423. {
  1424. [self showSleepTimer];
  1425. }
  1426. @end