VLCMovieViewController.m 67 KB

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