VLCMovieViewController.m 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. 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. 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 PlayingExternallyView *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. _doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecognized:)];
  213. [_doubleTapRecognizer setNumberOfTapsRequired:2];
  214. [self.view addGestureRecognizer:_doubleTapRecognizer];
  215. [_tapOnVideoRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];
  216. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togglePlayPause)];
  217. [_tapRecognizer setNumberOfTouchesRequired:2];
  218. _currentPanType = VLCPanTypeNone;
  219. _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
  220. [_panRecognizer setMinimumNumberOfTouches:1];
  221. [_panRecognizer setMaximumNumberOfTouches:1];
  222. _swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  223. _swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  224. _swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  225. _swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
  226. _swipeRecognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  227. _swipeRecognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
  228. _swipeRecognizerUp.numberOfTouchesRequired = 2;
  229. _swipeRecognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  230. _swipeRecognizerDown.direction = UISwipeGestureRecognizerDirectionDown;
  231. _swipeRecognizerDown.numberOfTouchesRequired = 2;
  232. [self.view addGestureRecognizer:_pinchRecognizer];
  233. [self.view addGestureRecognizer:_swipeRecognizerLeft];
  234. [self.view addGestureRecognizer:_swipeRecognizerRight];
  235. [self.view addGestureRecognizer:_swipeRecognizerUp];
  236. [self.view addGestureRecognizer:_swipeRecognizerDown];
  237. [self.view addGestureRecognizer:_panRecognizer];
  238. [self.view addGestureRecognizer:_tapRecognizer];
  239. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerLeft];
  240. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerRight];
  241. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerUp];
  242. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerDown];
  243. _panRecognizer.delegate = self;
  244. _swipeRecognizerRight.delegate = self;
  245. _swipeRecognizerLeft.delegate = self;
  246. _swipeRecognizerUp.delegate = self;
  247. _swipeRecognizerDown.delegate = self;
  248. _tapRecognizer.delegate = self;
  249. _doubleTapRecognizer.delegate = self;
  250. }
  251. - (void)setupControlPanel
  252. {
  253. _controllerPanel = [[VLCMovieViewControlPanelView alloc] initWithFrame:CGRectZero];
  254. [_controllerPanel.bwdButton addTarget:self action:@selector(backward:) forControlEvents:UIControlEventTouchUpInside];
  255. [_controllerPanel.fwdButton addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];
  256. [_controllerPanel.playPauseButton addTarget:self action:@selector(playPause) forControlEvents:UIControlEventTouchUpInside];
  257. [_controllerPanel.moreActionsButton addTarget:self action:@selector(moreActions:) forControlEvents:UIControlEventTouchUpInside];
  258. [_controllerPanel.playbackSpeedButton addTarget:self action:@selector(showPlaybackSpeedView) forControlEvents:UIControlEventTouchUpInside];
  259. [_controllerPanel.trackSwitcherButton addTarget:self action:@selector(switchTrack:) forControlEvents:UIControlEventTouchUpInside];
  260. [_controllerPanel.videoFilterButton addTarget:self action:@selector(videoFilterToggle:) forControlEvents:UIControlEventTouchUpInside];
  261. // HACK: get the slider from volume view
  262. UISlider *volumeSlider = nil;
  263. for (id aView in _controllerPanel.volumeView.subviews){
  264. if ([aView isKindOfClass:[UISlider class]]){
  265. volumeSlider = (UISlider *)aView;
  266. break;
  267. }
  268. }
  269. [volumeSlider addTarget:self action:@selector(volumeSliderAction:) forControlEvents:UIControlEventValueChanged];
  270. _controllerPanel.translatesAutoresizingMaskIntoConstraints = NO;
  271. [self.view addSubview:_controllerPanel];
  272. }
  273. - (void)setupConstraints
  274. {
  275. NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[panel]|"
  276. options:0
  277. metrics:nil
  278. views:@{@"panel":_controllerPanel}];
  279. NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[panel]|"
  280. options:0
  281. metrics:nil
  282. views:@{@"panel":_controllerPanel}];
  283. [self.view addConstraints:hConstraints];
  284. [self.view addConstraints:vConstraints];
  285. //constraint within _trackSelectorContainer is setting it's height to the tableviews contentview
  286. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:2.0/3.0 constant:0];
  287. widthConstraint.priority = UILayoutPriorityRequired - 1;
  288. NSArray *constraints = @[
  289. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
  290. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  291. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:420.0],
  292. widthConstraint,
  293. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:2.0/3.0 constant:0],
  294. ];
  295. [NSLayoutConstraint activateConstraints:constraints];
  296. }
  297. - (UIButton *)doneButton
  298. {
  299. if (!_doneButton) {
  300. _doneButton = [[UIButton alloc] initWithFrame:CGRectZero];
  301. [_doneButton setAccessibilityIdentifier:@"Done"];
  302. [_doneButton addTarget:self action:@selector(closePlayback:) forControlEvents:UIControlEventTouchUpInside];
  303. [_doneButton setTitle:NSLocalizedString(@"BUTTON_DONE", nil) forState:UIControlStateNormal];
  304. [_doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  305. _doneButton.translatesAutoresizingMaskIntoConstraints = NO;
  306. }
  307. return _doneButton;
  308. }
  309. - (UIStackView *)navigationBarStackView
  310. {
  311. if (!_navigationBarStackView) {
  312. _navigationBarStackView = [[UIStackView alloc] init];
  313. _navigationBarStackView.translatesAutoresizingMaskIntoConstraints = NO;
  314. _navigationBarStackView.spacing = 8;
  315. _navigationBarStackView.axis = UILayoutConstraintAxisHorizontal;
  316. _navigationBarStackView.alignment = UIStackViewAlignmentCenter;
  317. [_navigationBarStackView addArrangedSubview:self.doneButton];
  318. [_navigationBarStackView addArrangedSubview:_timeNavigationTitleView];
  319. [_navigationBarStackView addArrangedSubview:_rendererButton];
  320. }
  321. return _navigationBarStackView;
  322. }
  323. - (void)setupNavigationbar
  324. {
  325. if (!self.timeNavigationTitleView) {
  326. self.timeNavigationTitleView = [[[NSBundle mainBundle] loadNibNamed:@"VLCTimeNavigationTitleView" owner:self options:nil] objectAtIndex:0];
  327. self.timeNavigationTitleView.translatesAutoresizingMaskIntoConstraints = NO;
  328. }
  329. if (_vpc.renderer != nil) {
  330. [_rendererButton setSelected:YES];
  331. }
  332. if (self.navigationBarStackView.superview == nil) {
  333. [self.navigationController.navigationBar addSubview:self.navigationBarStackView];
  334. NSObject *guide = self.navigationController.navigationBar;
  335. if (@available(iOS 11.0, *)) {
  336. guide = self.navigationController.navigationBar.layoutMarginsGuide;
  337. }
  338. [NSLayoutConstraint activateConstraints:@[
  339. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  340. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeLeft multiplier:1 constant:8],
  341. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeRight multiplier:1 constant:-8],
  342. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  343. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  344. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.navigationBarStackView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]
  345. ]];
  346. }
  347. }
  348. - (void)resetVideoFiltersSliders
  349. {
  350. _brightnessSlider.value = 1.;
  351. _contrastSlider.value = 1.;
  352. _hueSlider.value = 0.;
  353. _saturationSlider.value = 1.;
  354. _gammaSlider.value = 1.;
  355. }
  356. - (void)viewWillAppear:(BOOL)animated
  357. {
  358. [super viewWillAppear:animated];
  359. _vpc.delegate = self;
  360. _lockedOrientation = UIInterfaceOrientationPortrait;
  361. [_vpc recoverPlaybackState];
  362. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  363. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  364. [self setupNavigationbar];
  365. /* reset audio meta data views */
  366. self.artworkImageView.image = nil;
  367. self.trackNameLabel.text = nil;
  368. self.artistNameLabel.text = nil;
  369. self.albumNameLabel.text = nil;
  370. [self setControlsHidden:NO animated:animated];
  371. [self updateDefaults];
  372. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDefaults) name:NSUserDefaultsDidChangeNotification object:nil];
  373. VLCRendererDiscovererManager *manager = _services.rendererDiscovererManager;
  374. manager.presentingViewController = self;
  375. manager.delegate = self;
  376. if ([_vpc isPlayingOnExternalScreen]) {
  377. [self showOnDisplay:_playingExternalView.displayView];
  378. }
  379. }
  380. - (void)viewDidAppear:(BOOL)animated
  381. {
  382. [super viewDidAppear:animated];
  383. _viewAppeared = YES;
  384. _playbackWillClose = NO;
  385. [_vpc recoverDisplayedMetadata];
  386. [self resetVideoFiltersSliders];
  387. _vpc.videoOutputView = self.movieView;
  388. _multiSelectionView.repeatMode = _vpc.repeatMode;
  389. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  390. //Media is loaded in the media player, checking the projection type and configuring accordingly.
  391. [self setupForMediaProjection];
  392. }
  393. - (void)viewDidLayoutSubviews
  394. {
  395. CGRect equalizerRect = _equalizerView.frame;
  396. equalizerRect.origin.x = CGRectGetMidX(self.view.bounds) - CGRectGetWidth(equalizerRect)/2.0;
  397. equalizerRect.origin.y = CGRectGetMidY(self.view.bounds) - CGRectGetHeight(equalizerRect)/2.0;
  398. _equalizerView.frame = equalizerRect;
  399. CGRect multiSelectionFrame;
  400. CGRect controllerPanelFrame = _controllerPanel.frame;;
  401. if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
  402. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  403. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  404. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  405. _multiSelectionView.frame = multiSelectionFrame;
  406. _multiSelectionView.showsEqualizer = YES;
  407. return;
  408. }
  409. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
  410. _multiSelectionView.showsEqualizer = YES;
  411. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  412. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  413. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  414. } else {
  415. _multiSelectionView.showsEqualizer = NO;
  416. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  417. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  418. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  419. }
  420. _multiSelectionView.frame = multiSelectionFrame;
  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 (_isTapSeeking)
  673. _numberOfTapSeek = 0;
  674. [self setControlsHidden:!_controlsHidden animated:YES];
  675. }
  676. - (void)updateActivityIndicatorForState:(VLCMediaPlayerState)state
  677. {
  678. if (state == VLCMediaPlayerStatePlaying || state == VLCMediaPlayerStatePaused) {
  679. _previousPlayerStateWasPaused = state == VLCMediaPlayerStatePaused;
  680. }
  681. BOOL shouldAnimate = state == VLCMediaPlayerStateBuffering && !_previousPlayerStateWasPaused;
  682. if (self.activityIndicator.isAnimating != shouldAnimate) {
  683. self.activityIndicator.alpha = shouldAnimate ? 1.0 : 0.0;
  684. shouldAnimate ? [self.activityIndicator startAnimating] : [self.activityIndicator stopAnimating];
  685. }
  686. }
  687. - (void)_resetIdleTimer
  688. {
  689. if (!_idleTimer)
  690. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:4.
  691. target:self
  692. selector:@selector(idleTimerExceeded)
  693. userInfo:nil
  694. repeats:NO];
  695. else {
  696. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 4.)
  697. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:4.]];
  698. }
  699. }
  700. - (NSString *)_stringInTimeFormatFrom:(int)duration
  701. {
  702. if (duration < 60) {
  703. return [NSString stringWithFormat:@"%is", duration];
  704. } else {
  705. return [NSString stringWithFormat:@"%im%is", duration / 60, duration % 60];
  706. }
  707. }
  708. - (void)_seekFromTap
  709. {
  710. NSMutableString *hudString = [NSMutableString string];
  711. int seekDuration = (int)_numberOfTapSeek * SHORT_JUMP_DURATION;
  712. if (seekDuration > 0) {
  713. [_vpc jumpForward:SHORT_JUMP_DURATION];
  714. [hudString appendString:@"⇒ "];
  715. _previousJumpState = VLCMovieJumpStateForward;
  716. } else {
  717. [_vpc jumpBackward:SHORT_JUMP_DURATION];
  718. [hudString appendString:@"⇐ "];
  719. _previousJumpState = VLCMovieJumpStateBackward;
  720. }
  721. [hudString appendString: [self _stringInTimeFormatFrom:abs(seekDuration)]];
  722. [self.statusLabel showStatusMessage:[NSString stringWithString:hudString]];
  723. if (_controlsHidden)
  724. [self setControlsHidden:NO animated:NO];
  725. }
  726. - (void)idleTimerExceeded
  727. {
  728. if (![NSThread isMainThread]) {
  729. [self performSelectorOnMainThread:@selector(idleTimerExceeded) withObject:nil waitUntilDone:NO];
  730. return;
  731. }
  732. _idleTimer = nil;
  733. if (!_controlsHidden)
  734. [self toggleControlsVisible];
  735. if (_isTapSeeking) {
  736. _isTapSeeking = NO;
  737. _numberOfTapSeek = 0;
  738. }
  739. if (!_videoFiltersHidden)
  740. _videoFiltersHidden = YES;
  741. if (_equalizerView.hidden == NO)
  742. _equalizerView.hidden = YES;
  743. if (!_playbackSpeedViewHidden)
  744. _playbackSpeedViewHidden = YES;
  745. if (self.scrubIndicatorView.hidden == NO)
  746. self.scrubIndicatorView.hidden = YES;
  747. }
  748. - (UIResponder *)nextResponder
  749. {
  750. [self _resetIdleTimer];
  751. return [super nextResponder];
  752. }
  753. - (VLCDeviceMotion *)deviceMotion
  754. {
  755. if (!_deviceMotion) {
  756. _deviceMotion = [VLCDeviceMotion new];
  757. _deviceMotion.delegate = self;
  758. }
  759. return _deviceMotion;
  760. }
  761. - (void)setupForMediaProjection
  762. {
  763. BOOL mediaHasProjection = [_vpc currentMediaIs360Video];
  764. _fov = mediaHasProjection ? DEFAULT_FOV : 0.f;
  765. [_swipeRecognizerUp setEnabled:!mediaHasProjection];
  766. [_swipeRecognizerDown setEnabled:!mediaHasProjection];
  767. [_swipeRecognizerLeft setEnabled:!mediaHasProjection];
  768. [_swipeRecognizerRight setEnabled:!mediaHasProjection];
  769. if (mediaHasProjection) {
  770. [self.deviceMotion startDeviceMotion];
  771. }
  772. }
  773. - (void)applyYaw:(CGFloat)diffYaw pitch:(CGFloat)diffPitch;
  774. {
  775. //Add and limit new pitch and yaw
  776. self.deviceMotion.yaw += diffYaw;
  777. self.deviceMotion.pitch += diffPitch;
  778. [_vpc updateViewpoint:self.deviceMotion.yaw pitch:self.deviceMotion.pitch roll:0 fov:_fov absolute:YES];
  779. }
  780. - (void)deviceMotionHasAttitudeWithDeviceMotion:(VLCDeviceMotion *)deviceMotion pitch:(double)pitch yaw:(double)yaw
  781. {
  782. if (_panRecognizer.state != UIGestureRecognizerStateChanged || UIGestureRecognizerStateBegan) {
  783. [self applyYaw:yaw pitch:pitch];
  784. }
  785. }
  786. #pragma mark - controls
  787. - (IBAction)closePlayback:(id)sender
  788. {
  789. _playbackWillClose = YES;
  790. [_vpc stopPlayback];
  791. }
  792. - (IBAction)minimizePlayback:(id)sender
  793. {
  794. [_delegate movieViewControllerDidSelectMinimize:self];
  795. }
  796. - (IBAction)positionSliderAction:(UISlider *)sender
  797. {
  798. /* we need to limit the number of events sent by the slider, since otherwise, the user
  799. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  800. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  801. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  802. if (_mediaDuration > 0) {
  803. VLCTime *newPosition = [VLCTime timeWithInt:(int)(sender.value * _mediaDuration)];
  804. [self.timeNavigationTitleView.timeDisplayButton setTitle:newPosition.stringValue forState:UIControlStateNormal];
  805. [self.timeNavigationTitleView setNeedsLayout];
  806. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"PLAYBACK_POSITION", nil), newPosition.stringValue];
  807. _positionSet = NO;
  808. }
  809. [self _resetIdleTimer];
  810. }
  811. - (void)_setPositionForReal
  812. {
  813. if (!_positionSet) {
  814. [_vpc setPlaybackPosition:self.timeNavigationTitleView.positionSlider.value];
  815. [_vpc setNeedsMetadataUpdate];
  816. _positionSet = YES;
  817. }
  818. }
  819. - (IBAction)positionSliderTouchDown:(id)sender
  820. {
  821. [self _updateScrubLabel];
  822. self.scrubIndicatorView.hidden = NO;
  823. _isScrubbing = YES;
  824. }
  825. - (IBAction)positionSliderTouchUp:(id)sender
  826. {
  827. self.scrubIndicatorView.hidden = YES;
  828. _isScrubbing = NO;
  829. }
  830. - (void)_updateScrubLabel
  831. {
  832. float speed = self.timeNavigationTitleView.positionSlider.scrubbingSpeed;
  833. if (speed == 1.)
  834. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", nil);
  835. else if (speed == .5)
  836. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", nil);
  837. else if (speed == .25)
  838. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", nil);
  839. else
  840. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", nil);
  841. [self _resetIdleTimer];
  842. }
  843. - (IBAction)positionSliderDrag:(id)sender
  844. {
  845. [self _updateScrubLabel];
  846. }
  847. - (void)volumeSliderAction:(id)sender
  848. {
  849. [self _resetIdleTimer];
  850. }
  851. - (void)updateTimeDisplayButton
  852. {
  853. UIButton *timeDisplayButton = self.timeNavigationTitleView.timeDisplayButton;
  854. if (_displayRemainingTime)
  855. [timeDisplayButton setTitle:[[_vpc remainingTime] stringValue] forState:UIControlStateNormal];
  856. else
  857. [timeDisplayButton setTitle:[[_vpc playedTime] stringValue] forState:UIControlStateNormal];
  858. [self.timeNavigationTitleView setNeedsLayout];
  859. }
  860. - (void)updateSleepTimerButton
  861. {
  862. NSMutableString *title = [NSMutableString stringWithString:NSLocalizedString(@"BUTTON_SLEEP_TIMER", nil)];
  863. if (_vpc.sleepTimer != nil && _vpc.sleepTimer.valid) {
  864. int remainSeconds = (int)[_vpc.sleepTimer.fireDate timeIntervalSinceNow];
  865. int hour = remainSeconds / 3600;
  866. int minute = (remainSeconds - hour * 3600) / 60;
  867. int second = remainSeconds % 60;
  868. [title appendFormat:@" %02d:%02d:%02d", hour, minute, second];
  869. } else {
  870. [_sleepCountDownTimer invalidate];
  871. }
  872. [_sleepTimerButton setTitle:title forState:UIControlStateNormal];
  873. }
  874. #pragma mark - playback controller delegation
  875. - (void)playbackPositionUpdated:(VLCPlaybackController *)controller
  876. {
  877. // FIXME: hard coded state since the state in mediaPlayer is incorrectly still buffering
  878. [self updateActivityIndicatorForState:VLCMediaPlayerStatePlaying];
  879. if (!_isScrubbing) {
  880. self.timeNavigationTitleView.positionSlider.value = [controller playbackPosition];
  881. }
  882. [self updateTimeDisplayButton];
  883. }
  884. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  885. {
  886. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  887. self.timeNavigationTitleView.positionSlider.value = 0.;
  888. [self.timeNavigationTitleView.timeDisplayButton setTitle:@"" forState:UIControlStateNormal];
  889. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = @"";
  890. [_equalizerView reloadData];
  891. double playbackRate = controller.playbackRate;
  892. self.playbackSpeedSlider.value = log2(playbackRate);
  893. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", playbackRate];
  894. float audioDelay = controller.audioDelay;
  895. self.audioDelaySlider.value = audioDelay;
  896. self.audioDelayIndicator.text = [NSString stringWithFormat:@"%d ms", (int) audioDelay];
  897. float subtitleDelay = controller.subtitleDelay;
  898. self.spuDelaySlider.value = subtitleDelay;
  899. self.spuDelayIndicator.text = [NSString stringWithFormat:@"%d ms", (int) subtitleDelay];
  900. [self _resetIdleTimer];
  901. }
  902. - (void)playbackDidStop:(NSNotification *)notification
  903. {
  904. [self minimizePlayback:nil];
  905. }
  906. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  907. isPlaying:(BOOL)isPlaying
  908. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  909. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  910. forPlaybackController:(VLCPlaybackController *)controller
  911. {
  912. [self updateActivityIndicatorForState:currentState];
  913. if (currentState == VLCMediaPlayerStateBuffering)
  914. _mediaDuration = controller.mediaDuration;
  915. if (currentState == VLCMediaPlayerStateError)
  916. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", nil)];
  917. [_controllerPanel updateButtons];
  918. _multiSelectionView.mediaHasChapters = currentMediaHasChapters;
  919. }
  920. - (void)showStatusMessage:(NSString *)statusMessage
  921. {
  922. [self.statusLabel showStatusMessage:statusMessage];
  923. }
  924. - (void)hideShowAspectratioButton:(BOOL)hide
  925. {
  926. [UIView animateWithDuration:.3
  927. animations:^{
  928. self.widthConstraint.constant = hide ? 0 : 30;
  929. self.timeNavigationTitleView.aspectRatioButton.hidden = hide;
  930. }];
  931. }
  932. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller metadata:(VLCMetaData *)metadata
  933. {
  934. if (!_viewAppeared)
  935. return;
  936. self.trackNameLabel.text = metadata.title;
  937. self.artworkImageView.image = metadata.artworkImage;
  938. if (!metadata.artworkImage) {
  939. self.artistNameLabel.text = metadata.artist;
  940. self.albumNameLabel.text = metadata.albumName;
  941. } else
  942. self.artistNameLabel.text = self.albumNameLabel.text = nil;
  943. [self hideShowAspectratioButton:metadata.isAudioOnly];
  944. [_controllerPanel updateButtons];
  945. [_playingExternalView updateUIWithRendererItem:_vpc.renderer title:metadata.title];
  946. _audioOnly = metadata.isAudioOnly;
  947. }
  948. - (IBAction)playPause
  949. {
  950. [_vpc playPause];
  951. }
  952. - (IBAction)forward:(id)sender
  953. {
  954. [_vpc next];
  955. }
  956. - (IBAction)backward:(id)sender
  957. {
  958. [_vpc previous];
  959. }
  960. - (IBAction)switchTrack:(id)sender
  961. {
  962. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == NO) {
  963. _trackSelectorContainer.switchingTracksNotChapters = YES;
  964. _trackSelectorContainer.hidden = NO;
  965. _trackSelectorContainer.alpha = 1.;
  966. [_trackSelectorContainer updateView];
  967. if (_equalizerView.hidden == NO)
  968. _equalizerView.hidden = YES;
  969. if (!_playbackSpeedViewHidden)
  970. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  971. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  972. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  973. [recognizer setEnabled:NO];
  974. [_tapOnVideoRecognizer setEnabled:YES];
  975. } else {
  976. _trackSelectorContainer.hidden = YES;
  977. _trackSelectorContainer.switchingTracksNotChapters = NO;
  978. }
  979. }
  980. - (IBAction)toggleTimeDisplay:(id)sender
  981. {
  982. _displayRemainingTime = !_displayRemainingTime;
  983. [self updateTimeDisplayButton];
  984. [self _resetIdleTimer];
  985. }
  986. - (IBAction)sleepTimer:(id)sender
  987. {
  988. if (!_playbackSpeedViewHidden)
  989. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  990. if (_equalizerView.hidden == NO)
  991. _equalizerView.hidden = YES;
  992. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  993. if (!_controlsHidden) {
  994. self.controllerPanel.hidden = _controlsHidden = YES;
  995. }
  996. }
  997. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  998. _sleepTimerContainer.alpha = 1.;
  999. _sleepTimerContainer.hidden = NO;
  1000. }
  1001. - (IBAction)sleepTimerCancel:(id)sender
  1002. {
  1003. NSTimer *sleepTimer = [_vpc sleepTimer];
  1004. if (sleepTimer) {
  1005. [sleepTimer invalidate];
  1006. sleepTimer = nil;
  1007. }
  1008. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  1009. [self setControlsHidden:YES animated:YES];
  1010. }
  1011. - (IBAction)sleepTimerAction:(id)sender
  1012. {
  1013. [_vpc scheduleSleepTimerWithInterval:_sleepTimeDatePicker.countDownDuration];
  1014. if (_sleepCountDownTimer == nil || _sleepCountDownTimer.valid == NO) {
  1015. _sleepCountDownTimer = [NSTimer scheduledTimerWithTimeInterval:1
  1016. target:self
  1017. selector:@selector(updateSleepTimerButton)
  1018. userInfo:nil
  1019. repeats:YES];
  1020. }
  1021. [self.statusLabel showStatusMessage:NSLocalizedString(@"SLEEP_TIMER_UPDATED", nil)];
  1022. [self setControlsHidden:YES animated:YES];
  1023. }
  1024. - (void)moreActions:(UIButton *)sender
  1025. {
  1026. if (_multiSelectionView.hidden == NO) {
  1027. [UIView animateWithDuration:.3
  1028. animations:^{
  1029. self->_multiSelectionView.hidden = YES;
  1030. }
  1031. completion:^(BOOL finished){
  1032. }];
  1033. return;
  1034. }
  1035. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1036. if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
  1037. _multiSelectionView.showsEqualizer = YES;
  1038. } else {
  1039. _multiSelectionView.showsEqualizer = NO;
  1040. }
  1041. }
  1042. CGRect workFrame = _multiSelectionView.frame;
  1043. workFrame.size = [_multiSelectionView proposedDisplaySize];
  1044. workFrame.origin.x = CGRectGetMaxX(sender.frame) - workFrame.size.width;
  1045. _multiSelectionView.alpha = 1.0f;
  1046. /* animate */
  1047. _multiSelectionView.frame = CGRectMake(workFrame.origin.x, workFrame.origin.y + workFrame.size.height, workFrame.size.width, 0.);
  1048. [UIView animateWithDuration:.3
  1049. animations:^{
  1050. self->_multiSelectionView.frame = workFrame;
  1051. self->_multiSelectionView.hidden = NO;
  1052. }
  1053. completion:^(BOOL finished){
  1054. }];
  1055. [self _resetIdleTimer];
  1056. }
  1057. #pragma mark - multi-select delegation
  1058. - (void)toggleUILock
  1059. {
  1060. _interfaceIsLocked = !_interfaceIsLocked;
  1061. if (_interfaceIsLocked) {
  1062. _lockedOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1063. }
  1064. NSArray *items = [self itemsForInterfaceLock];
  1065. for (NSObject *item in items) {
  1066. if ([item isKindOfClass:[UIControl class]]) {
  1067. UIControl *control = (UIControl *)item;
  1068. control.enabled = !_interfaceIsLocked;
  1069. } else if ([item isKindOfClass:[UIGestureRecognizer class]]){
  1070. UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer *)item;
  1071. gestureRecognizer.enabled = !_interfaceIsLocked;
  1072. } else if ([item isKindOfClass:[VLCVolumeView class]]) {
  1073. //The MPVolumeview doesn't adjust it's UI when disabled so we need to set the alpha by hand
  1074. VLCVolumeView *view = (VLCVolumeView *)item;
  1075. view.userInteractionEnabled = !_interfaceIsLocked;
  1076. view.alpha = _interfaceIsLocked ? 0.5 : 1;
  1077. } else {
  1078. NSAssert(NO, @"class not handled");
  1079. }
  1080. }
  1081. _multiSelectionView.displayLock = _interfaceIsLocked;
  1082. }
  1083. - (void)toggleEqualizer
  1084. {
  1085. if (_equalizerView.hidden) {
  1086. if (!_playbackSpeedViewHidden)
  1087. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1088. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1089. if (!_controlsHidden) {
  1090. self.controllerPanel.hidden = _controlsHidden = YES;
  1091. self.navigationController.navigationBar.hidden = YES;
  1092. }
  1093. }
  1094. _trackSelectorContainer.hidden = YES;
  1095. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1096. _equalizerView.alpha = 1.;
  1097. _equalizerView.hidden = NO;
  1098. } else
  1099. _equalizerView.hidden = YES;
  1100. }
  1101. - (void)toggleChapterAndTitleSelector
  1102. {
  1103. if (_trackSelectorContainer.hidden == YES || _trackSelectorContainer.switchingTracksNotChapters == YES) {
  1104. _trackSelectorContainer.switchingTracksNotChapters = NO;
  1105. [_trackSelectorContainer updateView];
  1106. _trackSelectorContainer.hidden = NO;
  1107. _trackSelectorContainer.alpha = 1.;
  1108. if (_equalizerView.hidden == NO)
  1109. _equalizerView.hidden = YES;
  1110. if (!_playbackSpeedViewHidden)
  1111. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1112. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1113. if (!_controlsHidden) {
  1114. self.controllerPanel.hidden = _controlsHidden = YES;
  1115. }
  1116. }
  1117. _sleepTimerContainer.hidden = YES;
  1118. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1119. } else {
  1120. _trackSelectorContainer.hidden = YES;
  1121. }
  1122. }
  1123. - (void)toggleRepeatMode
  1124. {
  1125. [[VLCPlaybackController sharedInstance] toggleRepeatMode];
  1126. _multiSelectionView.repeatMode = [VLCPlaybackController sharedInstance].repeatMode;
  1127. }
  1128. - (void)toggleShuffleMode
  1129. {
  1130. _vpc.shuffleMode = !_vpc.isShuffleMode;
  1131. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  1132. }
  1133. - (void)hideMenu
  1134. {
  1135. [UIView animateWithDuration:.2
  1136. animations:^{
  1137. self->_multiSelectionView.hidden = YES;
  1138. }
  1139. completion:^(BOOL finished){
  1140. }];
  1141. [self _resetIdleTimer];
  1142. }
  1143. #pragma mark - multi-touch gestures
  1144. - (void)togglePlayPause
  1145. {
  1146. if (!_playPauseGestureEnabled)
  1147. return;
  1148. if (_vpc.isPlaying) {
  1149. [_vpc pause];
  1150. [self setControlsHidden:NO animated:_controlsHidden];
  1151. } else {
  1152. [_vpc play];
  1153. }
  1154. }
  1155. - (BOOL)prefersHomeIndicatorAutoHidden
  1156. {
  1157. return YES;
  1158. }
  1159. - (VLCPanType)detectPanTypeForPan:(UIPanGestureRecognizer*)panRecognizer
  1160. {
  1161. NSString *deviceType = [[UIDevice currentDevice] model];
  1162. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  1163. CGFloat windowWidth = CGRectGetWidth(window.bounds);
  1164. CGPoint location = [panRecognizer locationInView:window];
  1165. VLCPanType panType = VLCPanTypeVolume; // default or right side of the screen
  1166. if (location.x < windowWidth / 2)
  1167. panType = VLCPanTypeBrightness;
  1168. // only check for seeking gesture if on iPad , will overwrite last statements if true
  1169. if ([deviceType isEqualToString:@"iPad"] && location.y < 110) {
  1170. panType = VLCPanTypeSeek;
  1171. }
  1172. if ([_vpc currentMediaIs360Video]) {
  1173. panType = VLCPanTypeProjection;
  1174. }
  1175. return panType;
  1176. }
  1177. - (void)panRecognized:(UIPanGestureRecognizer*)panRecognizer
  1178. {
  1179. CGFloat panDirectionX = [panRecognizer velocityInView:self.view].x;
  1180. CGFloat panDirectionY = [panRecognizer velocityInView:self.view].y;
  1181. if (panRecognizer.state == UIGestureRecognizerStateBegan) {
  1182. _currentPanType = [self detectPanTypeForPan:panRecognizer];
  1183. if ([_vpc currentMediaIs360Video]) {
  1184. _saveLocation = [panRecognizer locationInView:self.view];
  1185. [_deviceMotion stopDeviceMotion];
  1186. }
  1187. }
  1188. switch (_currentPanType) {
  1189. case VLCPanTypeSeek: {
  1190. if (!_seekGestureEnabled)
  1191. return;
  1192. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1193. int timeRemaining = timeRemainingDouble;
  1194. if (panDirectionX > 0) {
  1195. if (timeRemaining > 2 ) // to not go outside duration , video will stop
  1196. [_vpc jumpForward:1];
  1197. } else
  1198. [_vpc jumpBackward:1];
  1199. break;
  1200. case VLCPanTypeVolume:
  1201. if (!_volumeGestureEnabled)
  1202. return;
  1203. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
  1204. #pragma clang diagnostic push
  1205. #pragma clang diagnostic ignored "-Wdeprecated"
  1206. // there is no replacement for .volume which we want to use since Apple's susggestion is to not use their overlays
  1207. // but switch to the volume slider exclusively. meh.
  1208. if (panDirectionY > 0)
  1209. musicPlayer.volume -= 0.01;
  1210. else
  1211. musicPlayer.volume += 0.01;
  1212. #pragma clang diagnostic pop
  1213. } break;
  1214. case VLCPanTypeBrightness: {
  1215. if (!_brightnessGestureEnabled)
  1216. return;
  1217. CGFloat brightness = [UIScreen mainScreen].brightness;
  1218. if (panDirectionY > 0)
  1219. brightness = brightness - 0.01;
  1220. else
  1221. brightness = brightness + 0.01;
  1222. // Sanity check since -[UIScreen brightness] does not go by 0.01 steps
  1223. if (brightness > 1.0)
  1224. brightness = 1.0;
  1225. else if (brightness < 0.0)
  1226. brightness = 0.0;
  1227. NSAssert(brightness >= 0 && brightness <= 1, @"Brightness must be within 0 and 1 (it is %f)", brightness);
  1228. [[UIScreen mainScreen] setBrightness:brightness];
  1229. NSString *brightnessHUD = [NSString stringWithFormat:@"%@: %@ %%", NSLocalizedString(@"VFILTER_BRIGHTNESS", nil), [[[NSString stringWithFormat:@"%f",(brightness*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  1230. [self.statusLabel showStatusMessage:brightnessHUD];
  1231. } break;
  1232. case VLCPanTypeProjection: {
  1233. [self updateProjectionWithPanRecognizer:panRecognizer];
  1234. } break;
  1235. case VLCPanTypeNone: {
  1236. } break;
  1237. }
  1238. if (panRecognizer.state == UIGestureRecognizerStateEnded) {
  1239. _currentPanType = VLCPanTypeNone;
  1240. if ([_vpc currentMediaIs360Video]) {
  1241. [_deviceMotion startDeviceMotion];
  1242. }
  1243. }
  1244. }
  1245. - (void)updateProjectionWithPanRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer
  1246. {
  1247. CGPoint newLocationInView = [panGestureRecognizer locationInView:self.view];
  1248. CGFloat diffX = newLocationInView.x - _saveLocation.x;
  1249. CGFloat diffY = newLocationInView.y - _saveLocation.y;
  1250. _saveLocation = newLocationInView;
  1251. //screenSizePixel width is used twice to get a constant speed on the movement.
  1252. CGFloat diffYaw = _fov * -diffX / _screenPixelSize.width;
  1253. CGFloat diffPitch = _fov * -diffY / _screenPixelSize.width;
  1254. [self applyYaw:diffYaw pitch:diffPitch];
  1255. }
  1256. - (void)swipeRecognized:(UISwipeGestureRecognizer*)swipeRecognizer
  1257. {
  1258. if (!_seekGestureEnabled)
  1259. return;
  1260. NSString * hudString = @" ";
  1261. int swipeForwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : FORWARD_SWIPE_DURATION;
  1262. int swipeBackwardDuration = (_variableJumpDurationEnabled) ? ((int)(_mediaDuration*0.001*0.05)) : BACKWARD_SWIPE_DURATION;
  1263. if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
  1264. double timeRemainingDouble = (-[_vpc remainingTime].intValue*0.001);
  1265. int timeRemaining = timeRemainingDouble;
  1266. if (swipeForwardDuration < timeRemaining) {
  1267. if (swipeForwardDuration < 1)
  1268. swipeForwardDuration = 1;
  1269. [_vpc jumpForward:swipeForwardDuration];
  1270. hudString = [NSString stringWithFormat:@"⇒ %is", swipeForwardDuration];
  1271. } else {
  1272. [_vpc jumpForward:(timeRemaining - 5)];
  1273. hudString = [NSString stringWithFormat:@"⇒ %is",(timeRemaining - 5)];
  1274. }
  1275. }
  1276. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
  1277. [_vpc jumpBackward:swipeBackwardDuration];
  1278. hudString = [NSString stringWithFormat:@"⇐ %is",swipeBackwardDuration];
  1279. }else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionUp) {
  1280. [self backward:self];
  1281. }
  1282. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionDown) {
  1283. [self forward:self];
  1284. }
  1285. if (swipeRecognizer.state == UIGestureRecognizerStateEnded) {
  1286. [self.statusLabel showStatusMessage:hudString];
  1287. }
  1288. }
  1289. - (void)doubleTapRecognized:(UITapGestureRecognizer *)tapRecognizer
  1290. {
  1291. if (!_seekGestureEnabled)
  1292. return;
  1293. CGFloat screenWidth = self.view.frame.size.width;
  1294. CGFloat backwardBoundary = screenWidth / 3.0;
  1295. CGFloat forwardBoundary = 2 * screenWidth / 3.0;
  1296. CGPoint tapPosition = [tapRecognizer locationInView:self.view];
  1297. //Handling seek reset if tap orientation changes.
  1298. if (tapPosition.x < backwardBoundary) {
  1299. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateForward ? -1 : _numberOfTapSeek - 1;
  1300. } else if (tapPosition.x > forwardBoundary){
  1301. _numberOfTapSeek = _previousJumpState == VLCMovieJumpStateBackward ? 1 : _numberOfTapSeek + 1;
  1302. } else {
  1303. [_vpc toggleFullScreen];
  1304. return;
  1305. }
  1306. _isTapSeeking = YES;
  1307. [self _seekFromTap];
  1308. }
  1309. - (void)equalizerViewReceivedUserInput
  1310. {
  1311. [self _resetIdleTimer];
  1312. }
  1313. #pragma mark - Video Filter UI
  1314. - (IBAction)videoFilterToggle:(id)sender
  1315. {
  1316. if (!_playbackSpeedViewHidden)
  1317. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  1318. if (_equalizerView.hidden == NO)
  1319. _equalizerView.hidden = YES;
  1320. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1321. if (!_controlsHidden) {
  1322. self.controllerPanel.hidden = _controlsHidden = YES;
  1323. }
  1324. }
  1325. self.videoFilterView.hidden = !_videoFiltersHidden;
  1326. _videoFiltersHidden = self.videoFilterView.hidden;
  1327. }
  1328. - (IBAction)videoFilterSliderAction:(id)sender
  1329. {
  1330. if (sender == self.hueSlider)
  1331. _vpc.hue = self.hueSlider.value;
  1332. else if (sender == self.contrastSlider)
  1333. _vpc.contrast = self.contrastSlider.value;
  1334. else if (sender == self.brightnessSlider)
  1335. _vpc.brightness = self.brightnessSlider.value;
  1336. else if (sender == self.saturationSlider)
  1337. _vpc.saturation = self.saturationSlider.value;
  1338. else if (sender == self.gammaSlider)
  1339. _vpc.gamma = self.gammaSlider.value;
  1340. else if (sender == self.resetVideoFilterButton) {
  1341. [self resetVideoFiltersSliders];
  1342. [_vpc resetFilters];
  1343. } else
  1344. APLog(@"unknown sender for videoFilterSliderAction");
  1345. [self _resetIdleTimer];
  1346. }
  1347. - (void)appBecameActive:(NSNotification *)aNotification
  1348. {
  1349. if ([_delegate movieViewControllerShouldBeDisplayed:self]) {
  1350. [_vpc recoverDisplayedMetadata];
  1351. if (_vpc.videoOutputView != self.movieView) {
  1352. _vpc.videoOutputView = self.movieView;
  1353. }
  1354. }
  1355. }
  1356. #pragma mark - playback view
  1357. - (IBAction)playbackSliderAction:(UISlider *)sender
  1358. {
  1359. if (sender == _playbackSpeedSlider) {
  1360. double speed = exp2(sender.value);
  1361. _vpc.playbackRate = speed;
  1362. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  1363. } else if (sender == _audioDelaySlider) {
  1364. int delay = ((int) round(sender.value / 50.)) * 50;
  1365. _vpc.audioDelay = delay;
  1366. [sender setValue:delay animated:NO];
  1367. _audioDelayIndicator.text = [NSString stringWithFormat:@"%d ms", delay];
  1368. } else if (sender == _spuDelaySlider) {
  1369. int delay = (int) (round(sender.value / 50.)) * 50;
  1370. _vpc.subtitleDelay = delay;
  1371. [sender setValue:delay animated:NO];
  1372. _spuDelayIndicator.text = [NSString stringWithFormat:@"%d ms", delay];
  1373. }
  1374. [self _resetIdleTimer];
  1375. }
  1376. - (IBAction)videoDimensionAction:(id)sender
  1377. {
  1378. if (sender == self.timeNavigationTitleView.aspectRatioButton) {
  1379. [[VLCPlaybackController sharedInstance] switchAspectRatio];
  1380. }
  1381. }
  1382. - (IBAction)showPlaybackSpeedView {
  1383. if (!_videoFiltersHidden)
  1384. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  1385. if (_equalizerView.hidden == NO)
  1386. _equalizerView.hidden = YES;
  1387. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  1388. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  1389. [self _resetIdleTimer];
  1390. }
  1391. #pragma mark - autorotation
  1392. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  1393. {
  1394. BOOL orientationIslocked = _interfaceIsLocked || [_vpc currentMediaIs360Video];
  1395. UIInterfaceOrientationMask maskFromOrientation = 1 << _lockedOrientation;
  1396. return orientationIslocked ? maskFromOrientation : UIInterfaceOrientationMaskAll;
  1397. }
  1398. - (BOOL)shouldAutorotate
  1399. {
  1400. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1401. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  1402. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  1403. }
  1404. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  1405. {
  1406. if (_vpc.isPlaying && _controlsHidden)
  1407. [self setControlsHidden:NO animated:YES];
  1408. }
  1409. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  1410. {
  1411. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1412. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  1413. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1414. if (self.artworkImageView.image)
  1415. self.trackNameLabel.hidden = YES;
  1416. if (!self->_equalizerView.hidden)
  1417. self->_equalizerView.hidden = YES;
  1418. } completion:nil];
  1419. }
  1420. }
  1421. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
  1422. {
  1423. if (_vpc.isPlaying && _controlsHidden)
  1424. [self setControlsHidden:NO animated:YES];
  1425. }
  1426. #pragma mark - External Display
  1427. - (void)showOnDisplay:(UIView *)view
  1428. {
  1429. // if we don't have a renderer we're mirroring and don't want to show the dialog
  1430. BOOL displayExternally = view != _movieView;
  1431. [_playingExternalView shouldDisplay:displayExternally movieView:_movieView];
  1432. _artworkImageView.hidden = displayExternally;
  1433. if (!displayExternally && _movieView.superview != self.view) {
  1434. [self.view addSubview:_movieView];
  1435. [self.view sendSubviewToBack:_movieView];
  1436. _movieView.frame = self.view.frame;
  1437. }
  1438. }
  1439. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  1440. {
  1441. [self showOnDisplay:_playingExternalView.displayView];
  1442. }
  1443. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  1444. {
  1445. [self showOnDisplay:_movieView];
  1446. }
  1447. #pragma mark - Renderers
  1448. - (void)setupRendererDiscovererManager
  1449. {
  1450. // Create a renderer button for VLCMovieViewController
  1451. _rendererButton = [_services.rendererDiscovererManager setupRendererButton];
  1452. // Setting rendererIcons to white since default is orange
  1453. _rendererButton.tintColor = [UIColor whiteColor];
  1454. [_rendererButton setImage:[UIImage imageNamed:@"renderer"] forState:UIControlStateNormal];
  1455. [_rendererButton setImage:[UIImage imageNamed:@"rendererFull"] forState:UIControlStateSelected];
  1456. __weak typeof(self) weakSelf = self;
  1457. [_services.rendererDiscovererManager addSelectionHandler:^(VLCRendererItem * item) {
  1458. __strong typeof(weakSelf) strongSelf = weakSelf;
  1459. if (item) {
  1460. [strongSelf showOnDisplay:strongSelf->_playingExternalView.displayView];
  1461. } else {
  1462. [strongSelf removedCurrentRendererItem:strongSelf->_vpc.renderer];
  1463. }
  1464. }];
  1465. }
  1466. #pragma mark - VLCRendererDiscovererManagerDelegate
  1467. - (void)removedCurrentRendererItem:(VLCRendererItem *)item
  1468. {
  1469. [self showOnDisplay:_movieView];
  1470. }
  1471. @end