VLCMovieViewController.m 68 KB

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