VLCMovieViewController.m 74 KB

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