VLCMovieViewController.m 67 KB

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