VLCMovieViewController.m 68 KB

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