VLCMovieViewController.m 67 KB

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