VLCMovieViewController.m 65 KB

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