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 "VLCPlaybackController.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, VLCPlaybackControllerDelegate, 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. VLCPlaybackController *_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) VLCService *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:(VLCService *)services
  119. {
  120. self = [super initWithNibName:nil bundle:nil];
  121. if (self) {
  122. NSAssert([services isKindOfClass:[VLCService class]], @"VLCPlayerDisplayController: Injected services class issue");
  123. _services = services;
  124. }
  125. return self;
  126. }
  127. - (void)viewDidLoad
  128. {
  129. [super viewDidLoad];
  130. _vpc = [VLCPlaybackController sharedInstance];
  131. self.extendedLayoutIncludesOpaqueBars = YES;
  132. self.edgesForExtendedLayout = UIRectEdgeAll;
  133. self.videoFilterView.hidden = YES;
  134. _videoFiltersHidden = YES;
  135. _hueLabel.text = NSLocalizedString(@"VFILTER_HUE", nil);
  136. _hueSlider.accessibilityLabel = _hueLabel.text;
  137. _contrastLabel.text = NSLocalizedString(@"VFILTER_CONTRAST", nil);
  138. _contrastSlider.accessibilityLabel = _contrastLabel.text;
  139. _brightnessLabel.text = NSLocalizedString(@"VFILTER_BRIGHTNESS", nil);
  140. _brightnessSlider.accessibilityLabel = _brightnessLabel.text;
  141. _saturationLabel.text = NSLocalizedString(@"VFILTER_SATURATION", nil);
  142. _saturationSlider.accessibilityLabel = _saturationLabel.text;
  143. _gammaLabel.text = NSLocalizedString(@"VFILTER_GAMMA", nil);
  144. _gammaSlider.accessibilityLabel = _gammaLabel.text;
  145. _resetVideoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER_RESET_BUTTON", nil);
  146. #if !NEW_UI
  147. [self setupMultiSelectionView];
  148. self.trackNameLabel.text = @"";
  149. #else
  150. [self setupVideoOptionsControlBar];
  151. _moreOptionsActionSheet = [[VLCMediaMoreOptionsActionSheet alloc] init];
  152. _moreOptionsActionSheet.moreOptionsDelegate = self;
  153. self.navigationController.navigationBar.hidden = YES;
  154. self.trackNameLabel.hidden = YES;
  155. [self setupMediaNavigationBar];
  156. #endif
  157. _scrubHelpLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HELP", nil);
  158. self.playbackSpeedView.hidden = YES;
  159. _playbackSpeedViewHidden = YES;
  160. _playbackSpeedView.delegate = self;
  161. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  162. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  163. name:UIScreenDidConnectNotification object:nil];
  164. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  165. name:UIScreenDidDisconnectNotification object:nil];
  166. [center addObserver:self
  167. selector:@selector(appBecameActive:)
  168. name:UIApplicationDidBecomeActiveNotification
  169. object:nil];
  170. [center addObserver:self
  171. selector:@selector(playbackDidStop:)
  172. name:VLCPlaybackControllerPlaybackDidStop
  173. object:nil];
  174. self.artistNameLabel.text = self.albumNameLabel.text = @"";
  175. _movieView.userInteractionEnabled = NO;
  176. [self setupGestureRecognizers];
  177. _isTapSeeking = NO;
  178. _previousJumpState = VLCMovieJumpStateDefault;
  179. _numberOfTapSeek = 0;
  180. CGRect rect = self.resetVideoFilterButton.frame;
  181. rect.origin.y = rect.origin.y + 5.;
  182. self.resetVideoFilterButton.frame = rect;
  183. if (@available(iOS 11.0, *)) {
  184. [self.movieView setAccessibilityIgnoresInvertColors:YES];
  185. }
  186. [self.movieView setAccessibilityIdentifier:@"Video Player Title"];
  187. [self.movieView setAccessibilityLabel:NSLocalizedString(@"VO_VIDEOPLAYER_TITLE", nil)];
  188. [self.movieView setAccessibilityHint:NSLocalizedString(@"VO_VIDEOPLAYER_DOUBLETAP", nil)];
  189. _trackSelectorContainer = [[VLCTrackSelectorView alloc] initWithFrame:CGRectZero];
  190. _trackSelectorContainer.hidden = YES;
  191. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  192. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  193. [recognizer setEnabled:YES];
  194. self->_trackSelectorContainer.hidden = YES;
  195. };
  196. _trackSelectorContainer.completionHandler = completionBlock;
  197. _trackSelectorContainer.translatesAutoresizingMaskIntoConstraints = NO;
  198. [self.view addSubview:_trackSelectorContainer];
  199. _equalizerView = [[VLCEqualizerView alloc] initWithFrame:CGRectMake(0, 0, 450., 240.)];
  200. _equalizerView.delegate = [VLCPlaybackController sharedInstance];
  201. _equalizerView.UIdelegate = self;
  202. _equalizerView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
  203. _equalizerView.hidden = YES;
  204. [self.view addSubview:_equalizerView];
  205. //Sleep Timer initialization
  206. [self sleepTimerInitializer];
  207. [self setupControlPanel];
  208. CGRect screenBounds = [[UIScreen mainScreen] bounds];
  209. _screenPixelSize = CGSizeMake(screenBounds.size.width, screenBounds.size.height);
  210. [self setupConstraints];
  211. [self setupRendererDiscovererManager];
  212. }
  213. - (void) setupMultiSelectionView
  214. {
  215. _multiSelectionView = [[VLCMultiSelectionMenuView alloc] init];
  216. _multiSelectionView.delegate = self;
  217. _multiSelectionView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  218. _multiSelectionView.hidden = YES;
  219. _multiSelectionView.repeatMode = _vpc.repeatMode;
  220. [self.view addSubview:_multiSelectionView];
  221. }
  222. - (void)setupVideoOptionsControlBar
  223. {
  224. _videoOptionsControlBar = [[VLCVideoOptionsControlBar alloc] init];
  225. _videoOptionsControlBar.delegate = self;
  226. _videoOptionsControlBar.repeatMode = _vpc.repeatMode;
  227. [self.view addSubview:_videoOptionsControlBar];
  228. }
  229. - (void)setupGestureRecognizers
  230. {
  231. _tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];
  232. _tapOnVideoRecognizer.delegate = self;
  233. [self.view addGestureRecognizer:_tapOnVideoRecognizer];
  234. _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  235. _pinchRecognizer.delegate = self;
  236. _doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecognized:)];
  237. [_doubleTapRecognizer setNumberOfTapsRequired:2];
  238. [self.view addGestureRecognizer:_doubleTapRecognizer];
  239. [_tapOnVideoRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];
  240. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(togglePlayPause)];
  241. [_tapRecognizer setNumberOfTouchesRequired:2];
  242. _currentPanType = VLCPanTypeNone;
  243. _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
  244. [_panRecognizer setMinimumNumberOfTouches:1];
  245. [_panRecognizer setMaximumNumberOfTouches:1];
  246. _swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  247. _swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  248. _swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  249. _swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
  250. _swipeRecognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  251. _swipeRecognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
  252. _swipeRecognizerUp.numberOfTouchesRequired = 2;
  253. _swipeRecognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  254. _swipeRecognizerDown.direction = UISwipeGestureRecognizerDirectionDown;
  255. _swipeRecognizerDown.numberOfTouchesRequired = 2;
  256. [self.view addGestureRecognizer:_pinchRecognizer];
  257. [self.view addGestureRecognizer:_swipeRecognizerLeft];
  258. [self.view addGestureRecognizer:_swipeRecognizerRight];
  259. [self.view addGestureRecognizer:_swipeRecognizerUp];
  260. [self.view addGestureRecognizer:_swipeRecognizerDown];
  261. [self.view addGestureRecognizer:_panRecognizer];
  262. [self.view addGestureRecognizer:_tapRecognizer];
  263. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerLeft];
  264. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerRight];
  265. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerUp];
  266. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerDown];
  267. _panRecognizer.delegate = self;
  268. _swipeRecognizerRight.delegate = self;
  269. _swipeRecognizerLeft.delegate = self;
  270. _swipeRecognizerUp.delegate = self;
  271. _swipeRecognizerDown.delegate = self;
  272. _tapRecognizer.delegate = self;
  273. _doubleTapRecognizer.delegate = self;
  274. }
  275. - (void)setupControlPanel
  276. {
  277. _controllerPanel = [[VLCMovieViewControlPanelView alloc] initWithFrame:CGRectZero];
  278. [_controllerPanel.bwdButton addTarget:self action:@selector(backward:) forControlEvents:UIControlEventTouchUpInside];
  279. [_controllerPanel.fwdButton addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];
  280. [_controllerPanel.playPauseButton addTarget:self action:@selector(playPause) forControlEvents:UIControlEventTouchUpInside];
  281. [_controllerPanel.moreActionsButton addTarget:self action:@selector(moreActions:) forControlEvents:UIControlEventTouchUpInside];
  282. [_controllerPanel.playbackSpeedButton addTarget:self action:@selector(showPlaybackSpeedView) forControlEvents:UIControlEventTouchUpInside];
  283. [_controllerPanel.trackSwitcherButton addTarget:self action:@selector(switchTrack:) forControlEvents:UIControlEventTouchUpInside];
  284. [_controllerPanel.videoFilterButton addTarget:self action:@selector(videoFilterToggle:) forControlEvents:UIControlEventTouchUpInside];
  285. // HACK: get the slider from volume view
  286. UISlider *volumeSlider = nil;
  287. for (id aView in _controllerPanel.volumeView.subviews){
  288. if ([aView isKindOfClass:[UISlider class]]){
  289. volumeSlider = (UISlider *)aView;
  290. break;
  291. }
  292. }
  293. [volumeSlider addTarget:self action:@selector(volumeSliderAction:) forControlEvents:UIControlEventValueChanged];
  294. _controllerPanel.translatesAutoresizingMaskIntoConstraints = NO;
  295. [self.view addSubview:_controllerPanel];
  296. }
  297. - (NSArray *) getMediaNavigationBarConstraints
  298. {
  299. UILayoutGuide *guide = self.view.layoutMarginsGuide;
  300. if (@available(iOS 11.0, *)) {
  301. guide = self.view.safeAreaLayoutGuide;
  302. }
  303. CGFloat padding = 20.0f;
  304. return @[
  305. [_mediaNavigationBar.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  306. [_mediaNavigationBar.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor constant:padding],
  307. [_mediaNavigationBar.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor constant: -padding],
  308. [_mediaNavigationBar.topAnchor constraintEqualToAnchor:guide.topAnchor constant: padding],
  309. ];
  310. }
  311. - (void)setupConstraints
  312. {
  313. NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[panel]|"
  314. options:0
  315. metrics:nil
  316. views:@{@"panel":_controllerPanel}];
  317. NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[panel]|"
  318. options:0
  319. metrics:nil
  320. views:@{@"panel":_controllerPanel}];
  321. [self.view addConstraints:hConstraints];
  322. [self.view addConstraints:vConstraints];
  323. //constraint within _trackSelectorContainer is setting it's height to the tableviews contentview
  324. NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:2.0/3.0 constant:0];
  325. widthConstraint.priority = UILayoutPriorityRequired - 1;
  326. NSArray *constraints = @[
  327. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
  328. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  329. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:420.0],
  330. widthConstraint,
  331. [NSLayoutConstraint constraintWithItem:_trackSelectorContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:2.0/3.0 constant:0],
  332. [_videoFilterView.bottomAnchor constraintEqualToAnchor:_controllerPanel.topAnchor]
  333. ];
  334. #if NEW_UI
  335. constraints = [constraints arrayByAddingObjectsFromArray:[self getVideoOptionsConstraints]];
  336. constraints = [constraints arrayByAddingObjectsFromArray:[self getMediaNavigationBarConstraints]];
  337. #endif
  338. [NSLayoutConstraint activateConstraints: constraints];
  339. }
  340. - (UIButton *)doneButton
  341. {
  342. if (!_doneButton) {
  343. _doneButton = [[UIButton alloc] initWithFrame:CGRectZero];
  344. [_doneButton setAccessibilityIdentifier:@"Done"];
  345. [_doneButton addTarget:self action:@selector(closePlayback:) forControlEvents:UIControlEventTouchUpInside];
  346. [_doneButton setTitle:NSLocalizedString(@"BUTTON_DONE", nil) forState:UIControlStateNormal];
  347. [_doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  348. _doneButton.translatesAutoresizingMaskIntoConstraints = NO;
  349. }
  350. return _doneButton;
  351. }
  352. - (UIStackView *)navigationBarStackView
  353. {
  354. if (!_navigationBarStackView) {
  355. _navigationBarStackView = [[UIStackView alloc] init];
  356. _navigationBarStackView.translatesAutoresizingMaskIntoConstraints = NO;
  357. _navigationBarStackView.spacing = 8;
  358. _navigationBarStackView.axis = UILayoutConstraintAxisHorizontal;
  359. _navigationBarStackView.alignment = UIStackViewAlignmentCenter;
  360. [_navigationBarStackView addArrangedSubview:self.doneButton];
  361. [_navigationBarStackView addArrangedSubview:_timeNavigationTitleView];
  362. [_navigationBarStackView addArrangedSubview:_rendererButton];
  363. }
  364. return _navigationBarStackView;
  365. }
  366. - (void) setupMediaNavigationBar {
  367. _mediaNavigationBar = [[VLCMediaNavigationBar alloc] init];
  368. _mediaNavigationBar.delegate = self;
  369. _mediaNavigationBar.chromeCastButton.hidden = _vpc.renderer == nil;
  370. [self.view addSubview:_mediaNavigationBar];
  371. }
  372. - (void)setupNavigationbar
  373. {
  374. if (!self.timeNavigationTitleView) {
  375. self.timeNavigationTitleView = [[[NSBundle mainBundle] loadNibNamed:@"VLCTimeNavigationTitleView" owner:self options:nil] objectAtIndex:0];
  376. self.timeNavigationTitleView.translatesAutoresizingMaskIntoConstraints = NO;
  377. }
  378. if (_vpc.renderer != nil) {
  379. [_rendererButton setSelected:YES];
  380. }
  381. if (self.navigationBarStackView.superview == nil) {
  382. [self.navigationController.navigationBar addSubview:self.navigationBarStackView];
  383. NSObject *guide = self.navigationController.navigationBar;
  384. if (@available(iOS 11.0, *)) {
  385. guide = self.navigationController.navigationBar.layoutMarginsGuide;
  386. }
  387. [NSLayoutConstraint activateConstraints:@[
  388. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
  389. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeLeft multiplier:1 constant:8],
  390. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:guide attribute:NSLayoutAttributeRight multiplier:1 constant:-8],
  391. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  392. [NSLayoutConstraint constraintWithItem:self.navigationBarStackView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  393. [NSLayoutConstraint constraintWithItem:self.timeNavigationTitleView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.navigationBarStackView attribute:NSLayoutAttributeHeight multiplier:1 constant:0],
  394. ]];
  395. self.scrubViewTopConstraint.constant = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  396. }
  397. }
  398. - (void)resetVideoFiltersSliders
  399. {
  400. _brightnessSlider.value = 1.;
  401. _contrastSlider.value = 1.;
  402. _hueSlider.value = 0.;
  403. _saturationSlider.value = 1.;
  404. _gammaSlider.value = 1.;
  405. }
  406. - (void)viewWillAppear:(BOOL)animated
  407. {
  408. [super viewWillAppear:animated];
  409. _vpc.delegate = self;
  410. _lockedOrientation = UIInterfaceOrientationPortrait;
  411. [_vpc recoverPlaybackState];
  412. #if !NEW_UI
  413. [self setupNavigationbar];
  414. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  415. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  416. self.trackNameLabel.text = nil;
  417. #else
  418. self.navigationController.navigationBar.hidden = YES;
  419. #endif
  420. /* reset audio meta data views */
  421. self.artworkImageView.image = nil;
  422. self.artistNameLabel.text = nil;
  423. self.albumNameLabel.text = nil;
  424. [self setControlsHidden:NO animated:animated];
  425. [self updateDefaults];
  426. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDefaults) name:NSUserDefaultsDidChangeNotification object:nil];
  427. VLCRendererDiscovererManager *manager = _services.rendererDiscovererManager;
  428. manager.presentingViewController = self;
  429. manager.delegate = self;
  430. if ([_vpc isPlayingOnExternalScreen]) {
  431. [self showOnDisplay:_playingExternalView.displayView];
  432. }
  433. }
  434. - (void)layoutFrameForMultiSelectionView
  435. {
  436. CGRect controllerPanelFrame = _controllerPanel.frame;
  437. CGRect multiSelectionFrame;
  438. BOOL isIphone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
  439. if (!isIphone) {
  440. _multiSelectionView.showsEqualizer = YES;
  441. } else {
  442. BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
  443. _multiSelectionView.showsEqualizer = isLandscape;
  444. }
  445. multiSelectionFrame = (CGRect){CGPointMake(0., 0.), [_multiSelectionView proposedDisplaySize]};
  446. multiSelectionFrame.origin.x = controllerPanelFrame.size.width - multiSelectionFrame.size.width;
  447. multiSelectionFrame.origin.y = controllerPanelFrame.origin.y - multiSelectionFrame.size.height;
  448. _multiSelectionView.frame = multiSelectionFrame;
  449. }
  450. - (void)viewDidAppear:(BOOL)animated
  451. {
  452. [super viewDidAppear:animated];
  453. _viewAppeared = YES;
  454. _playbackWillClose = NO;
  455. [_vpc recoverDisplayedMetadata];
  456. [self resetVideoFiltersSliders];
  457. _vpc.videoOutputView = self.movieView;
  458. #if !NEW_UI
  459. _multiSelectionView.repeatMode = _vpc.repeatMode;
  460. _multiSelectionView.shuffleMode = _vpc.isShuffleMode;
  461. #else
  462. _videoOptionsControlBar.repeatMode = _vpc.repeatMode;
  463. #endif
  464. //Media is loaded in the media player, checking the projection type and configuring accordingly.
  465. [self setupForMediaProjection];
  466. }
  467. - (void)viewDidLayoutSubviews
  468. {
  469. CGRect equalizerRect = _equalizerView.frame;
  470. equalizerRect.origin.x = CGRectGetMidX(self.view.bounds) - CGRectGetWidth(equalizerRect)/2.0;
  471. equalizerRect.origin.y = CGRectGetMidY(self.view.bounds) - CGRectGetHeight(equalizerRect)/2.0;
  472. _equalizerView.frame = equalizerRect;
  473. #if !NEW_UI
  474. [self layoutFrameForMultiSelectionView];
  475. #endif
  476. self.scrubViewTopConstraint.constant = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  477. }
  478. - (void)viewWillDisappear:(BOOL)animated
  479. {
  480. if (_vpc.videoOutputView == self.movieView) {
  481. _vpc.videoOutputView = nil;
  482. }
  483. _viewAppeared = NO;
  484. if (_idleTimer) {
  485. [_idleTimer invalidate];
  486. _idleTimer = nil;
  487. }
  488. [super viewWillDisappear:animated];
  489. // hide filter UI for next run
  490. if (!_videoFiltersHidden)
  491. _videoFiltersHidden = YES;
  492. if (_equalizerView.hidden == NO)
  493. _equalizerView.hidden = YES;
  494. if (!_playbackSpeedViewHidden)
  495. _playbackSpeedViewHidden = YES;
  496. if (_interfaceIsLocked)
  497. [self toggleUILock];
  498. // reset tap to seek values
  499. _isTapSeeking = NO;
  500. _previousJumpState = VLCMovieJumpStateDefault;
  501. _numberOfTapSeek = 0;
  502. [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
  503. [[NSUserDefaults standardUserDefaults] setBool:_displayRemainingTime forKey:kVLCShowRemainingTime];
  504. }
  505. - (void)viewDidDisappear:(BOOL)animated
  506. {
  507. [super viewDidDisappear:animated];
  508. [self.deviceMotion stopDeviceMotion];
  509. }
  510. - (BOOL)canBecomeFirstResponder
  511. {
  512. return YES;
  513. }
  514. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
  515. {
  516. [self setControlsHidden:YES animated:flag];
  517. [super dismissViewControllerAnimated:flag completion:completion];
  518. }
  519. - (void)updateDefaults
  520. {
  521. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  522. if (!_playbackWillClose) {
  523. _displayRemainingTime = [[defaults objectForKey:kVLCShowRemainingTime] boolValue];
  524. [self updateTimeDisplayButton];
  525. }
  526. _volumeGestureEnabled = [[defaults objectForKey:kVLCSettingVolumeGesture] boolValue];
  527. _playPauseGestureEnabled = [[defaults objectForKey:kVLCSettingPlayPauseGesture] boolValue];
  528. _brightnessGestureEnabled = [[defaults objectForKey:kVLCSettingBrightnessGesture] boolValue];
  529. _seekGestureEnabled = [[defaults objectForKey:kVLCSettingSeekGesture] boolValue];
  530. _closeGestureEnabled = [[defaults objectForKey:kVLCSettingCloseGesture] boolValue];
  531. _variableJumpDurationEnabled = [[defaults objectForKey:kVLCSettingVariableJumpDuration] boolValue];
  532. }
  533. #pragma mark - Initializer helper
  534. - (void)sleepTimerInitializer
  535. {
  536. /* add sleep timer UI */
  537. _sleepTimerContainer = [[VLCFrostedGlasView alloc] initWithFrame:CGRectMake(0., 0., 300., 200.)];
  538. _sleepTimerContainer.center = self.view.center;
  539. _sleepTimerContainer.clipsToBounds = YES;
  540. _sleepTimerContainer.layer.cornerRadius = 5;
  541. _sleepTimerContainer.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  542. //Layers used to create a separator for the buttons.
  543. CALayer *horizontalSeparator = [CALayer layer];
  544. horizontalSeparator.frame = CGRectMake(0., 162., 300., 1.);
  545. horizontalSeparator.backgroundColor = [UIColor VLCLightTextColor].CGColor;
  546. CALayer *verticalSeparator = [CALayer layer];
  547. verticalSeparator.frame = CGRectMake(150., 162., 1., 48.);
  548. verticalSeparator.backgroundColor = [UIColor VLCLightTextColor].CGColor;
  549. _sleepTimeDatePicker = [[UIDatePicker alloc] init];
  550. _sleepTimeDatePicker.opaque = NO;
  551. _sleepTimeDatePicker.backgroundColor = [UIColor clearColor];
  552. _sleepTimeDatePicker.tintColor = [UIColor VLCLightTextColor];
  553. _sleepTimeDatePicker.frame = CGRectMake(0., 0., 300., 162.);
  554. _sleepTimeDatePicker.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  555. [_sleepTimerContainer addSubview:_sleepTimeDatePicker];
  556. [_sleepTimerContainer.layer addSublayer:horizontalSeparator];
  557. [_sleepTimerContainer.layer addSublayer:verticalSeparator];
  558. UIButton *cancelSleepTimer = [[UIButton alloc] initWithFrame:CGRectMake(0., 162., 150., 48.)];
  559. cancelSleepTimer.backgroundColor = [UIColor clearColor];
  560. [cancelSleepTimer setTitle:NSLocalizedString(@"BUTTON_RESET", nil) forState:UIControlStateNormal];
  561. [cancelSleepTimer setTintColor:[UIColor VLCLightTextColor]];
  562. [cancelSleepTimer setTitleColor:[UIColor VLCDarkTextShadowColor] forState:UIControlStateHighlighted];
  563. [cancelSleepTimer addTarget:self action:@selector(sleepTimerCancel:) forControlEvents:UIControlEventTouchDown];
  564. cancelSleepTimer.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
  565. [_sleepTimerContainer addSubview:cancelSleepTimer];
  566. UIButton *confirmSleepTimer = [[UIButton alloc] initWithFrame:CGRectMake(150., 162., 150., 48.)];
  567. confirmSleepTimer.backgroundColor = [UIColor clearColor];
  568. [confirmSleepTimer setTitle:NSLocalizedString(@"BUTTON_SET", nil) forState:UIControlStateNormal];
  569. [confirmSleepTimer setTintColor:[UIColor VLCLightTextColor]];
  570. [confirmSleepTimer setTitleColor:[UIColor VLCDarkTextShadowColor] forState:UIControlStateHighlighted];
  571. [confirmSleepTimer addTarget:self action:@selector(sleepTimerAction:) forControlEvents:UIControlEventTouchDown];
  572. confirmSleepTimer.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
  573. [_sleepTimerContainer addSubview:confirmSleepTimer];
  574. /* adapt the date picker style to suit our needs */
  575. [_sleepTimeDatePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"];
  576. SEL selector = NSSelectorFromString(@"setHighlightsToday:");
  577. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
  578. BOOL no = NO;
  579. [invocation setSelector:selector];
  580. [invocation setArgument:&no atIndex:2];
  581. [invocation invokeWithTarget:_sleepTimeDatePicker];
  582. if (_sleepTimerContainer.subviews.count > 0) {
  583. NSArray *subviewsOfSubview = [_sleepTimeDatePicker.subviews[0] subviews];
  584. NSUInteger subviewCount = subviewsOfSubview.count;
  585. for (NSUInteger x = 0; x < subviewCount; x++) {
  586. if ([subviewsOfSubview[x] isKindOfClass:[UILabel class]])
  587. [subviewsOfSubview[x] setTextColor:[UIColor VLCLightTextColor]];
  588. }
  589. }
  590. _sleepTimeDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
  591. _sleepTimeDatePicker.minuteInterval = 1;
  592. _sleepTimeDatePicker.minimumDate = [NSDate date];
  593. _sleepTimeDatePicker.countDownDuration = 1200.;
  594. [self.view addSubview:_sleepTimerContainer];
  595. }
  596. #pragma mark - controls visibility
  597. - (NSArray *)itemsForInterfaceLock
  598. {
  599. NSArray *arr = [NSArray arrayWithObjects:
  600. _pinchRecognizer,
  601. _panRecognizer,
  602. _tapRecognizer,
  603. _doubleTapRecognizer,
  604. _controllerPanel.playbackSpeedButton,
  605. _controllerPanel.trackSwitcherButton,
  606. _controllerPanel.bwdButton,
  607. _controllerPanel.playPauseButton,
  608. _controllerPanel.fwdButton,
  609. _controllerPanel.videoFilterButton,
  610. _controllerPanel.volumeView,
  611. nil];
  612. #if !NEW_UI
  613. arr = [arr arrayByAddingObjectsFromArray:
  614. @[_multiSelectionView.repeatButton,
  615. _multiSelectionView.shuffleButton,
  616. _multiSelectionView.equalizerButton,
  617. _multiSelectionView.chapterSelectorButton,
  618. _timeNavigationTitleView.minimizePlaybackButton,
  619. _timeNavigationTitleView.positionSlider,
  620. _timeNavigationTitleView.aspectRatioButton,
  621. _doneButton,
  622. _rendererButton]];
  623. #else
  624. UIView *airplayView;
  625. if (@available(iOS 11.0, *)) {
  626. airplayView = (UIView *)_mediaNavigationBar.airplayRoutePickerView;
  627. } else {
  628. airplayView = (UIView *)_mediaNavigationBar.airplayVolumeView;
  629. }
  630. arr = [arr arrayByAddingObjectsFromArray:
  631. @[_videoOptionsControlBar.toggleFullScreenButton,
  632. _videoOptionsControlBar.selectSubtitleButton,
  633. _videoOptionsControlBar.repeatButton,
  634. _mediaNavigationBar.minimizePlaybackButton,
  635. _mediaNavigationBar.chromeCastButton,
  636. airplayView]
  637. ];
  638. #endif
  639. return arr;
  640. }
  641. - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
  642. {
  643. CGFloat diff = DEFAULT_FOV * -(ZOOM_SENSITIVITY * recognizer.velocity / _screenPixelSize.width);
  644. if ([_vpc currentMediaIs360Video]) {
  645. [self zoom360Video:diff];
  646. } else if (recognizer.velocity < 0. && _closeGestureEnabled) {
  647. [self minimizePlayback:nil];
  648. }
  649. }
  650. - (void)zoom360Video:(CGFloat)zoom
  651. {
  652. if ([_vpc updateViewpoint:0 pitch:0 roll:0 fov:zoom absolute:NO]) {
  653. //clamp Fov between min and max fov
  654. _fov = MAX(MIN(_fov + zoom, MAX_FOV), MIN_FOV);
  655. }
  656. }
  657. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  658. {
  659. if (touch.view != self.view)
  660. return NO;
  661. return YES;
  662. }
  663. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  664. {
  665. return YES;
  666. }
  667. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated
  668. {
  669. _controlsHidden = hidden;
  670. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  671. if (!_controlsHidden) {
  672. _controllerPanel.alpha = 0.0f;
  673. _controllerPanel.hidden = !_videoFiltersHidden;
  674. _videoFilterView.alpha = 0.0f;
  675. _videoFilterView.hidden = _videoFiltersHidden;
  676. _playbackSpeedView.alpha = 0.0f;
  677. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  678. _trackSelectorContainer.alpha = 0.0f;
  679. _trackSelectorContainer.hidden = YES;
  680. _equalizerView.alpha = 0.0f;
  681. _equalizerView.hidden = YES;
  682. if (_sleepTimerContainer) {
  683. _sleepTimerContainer.alpha = 0.0f;
  684. _sleepTimerContainer.hidden = YES;
  685. }
  686. #if !NEW_UI
  687. _multiSelectionView.alpha = 0.0f;
  688. _multiSelectionView.hidden = YES;
  689. self.navigationController.navigationBar.alpha = 0.0;
  690. self.navigationController.navigationBar.hidden = NO;
  691. _trackNameLabel.hidden = NO;
  692. #else
  693. _videoOptionsControlBar.alpha = 0.0f;
  694. _videoOptionsControlBar.hidden = YES;
  695. _mediaNavigationBar.alpha = 0.0f;
  696. _mediaNavigationBar.hidden = YES;
  697. #endif
  698. _artistNameLabel.hidden = NO;
  699. _albumNameLabel.hidden = NO;
  700. }
  701. void (^animationBlock)(void) = ^() {
  702. self->_controllerPanel.alpha = alpha;
  703. self->_videoFilterView.alpha = alpha;
  704. self->_playbackSpeedView.alpha = alpha;
  705. self->_trackSelectorContainer.alpha = alpha;
  706. CGFloat metaInfoAlpha = self->_audioOnly ? 1.0f : alpha;
  707. self->_artistNameLabel.alpha = metaInfoAlpha;
  708. self->_albumNameLabel.alpha = metaInfoAlpha;
  709. #if !NEW_UI
  710. self->_multiSelectionView.alpha = alpha;
  711. self.navigationController.navigationBar.alpha = alpha;
  712. self->_trackNameLabel.alpha = [self->_vpc isPlayingOnExternalScreen] ? 0.f : metaInfoAlpha;
  713. #else
  714. self->_videoOptionsControlBar.alpha = alpha;
  715. self->_mediaNavigationBar.alpha = alpha;
  716. #endif
  717. self->_equalizerView.alpha = alpha;
  718. if (self->_sleepTimerContainer)
  719. self->_sleepTimerContainer.alpha = alpha;
  720. };
  721. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  722. self->_controllerPanel.hidden = self->_videoFiltersHidden ? self->_controlsHidden : NO;
  723. self->_videoFilterView.hidden = self->_videoFiltersHidden;
  724. self->_playbackSpeedView.hidden = self->_playbackSpeedViewHidden;
  725. self->_trackSelectorContainer.hidden = YES;
  726. self->_equalizerView.hidden = YES;
  727. if (self->_sleepTimerContainer)
  728. self->_sleepTimerContainer.hidden = YES;
  729. #if !NEW_UI
  730. self->_multiSelectionView.hidden = YES;
  731. self.navigationController.navigationBar.hidden = self->_controlsHidden;
  732. self->_trackNameLabel.hidden = self->_audioOnly ? NO : self->_controlsHidden;
  733. #else
  734. self->_videoOptionsControlBar.hidden = NO;
  735. self->_mediaNavigationBar.hidden = self->_controlsHidden;
  736. #endif
  737. self->_artistNameLabel.hidden = self->_audioOnly ? NO : self->_controlsHidden;
  738. self->_albumNameLabel.hidden = self->_audioOnly ? NO : self->_controlsHidden;
  739. };
  740. NSTimeInterval animationDuration = animated? 0.3 : 0.0;
  741. [UIView animateWithDuration:animationDuration animations:^{
  742. [self setNeedsStatusBarAppearanceUpdate];
  743. }];
  744. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  745. }
  746. - (BOOL)prefersStatusBarHidden
  747. {
  748. return _viewAppeared ? _controlsHidden : NO;
  749. }
  750. - (UIStatusBarStyle)preferredStatusBarStyle
  751. {
  752. return UIStatusBarStyleLightContent;
  753. }
  754. - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
  755. {
  756. return UIStatusBarAnimationFade;
  757. }
  758. - (void)toggleControlsVisible
  759. {
  760. if (!_trackSelectorContainer.hidden) {
  761. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
  762. [recognizer setEnabled:YES];
  763. }
  764. if (_controlsHidden && !_videoFiltersHidden)
  765. _videoFiltersHidden = YES;
  766. if (_controlsHidden && !_playbackSpeedViewHidden)
  767. _playbackSpeedViewHidden = YES;
  768. if (_isTapSeeking)
  769. _numberOfTapSeek = 0;
  770. [self setControlsHidden:!_controlsHidden animated:YES];
  771. }
  772. - (void)updateActivityIndicatorForState:(VLCMediaPlayerState)state
  773. {
  774. if (state == VLCMediaPlayerStatePlaying || state == VLCMediaPlayerStatePaused) {
  775. _previousPlayerStateWasPaused = state == VLCMediaPlayerStatePaused;
  776. }
  777. BOOL shouldAnimate = state == VLCMediaPlayerStateBuffering && !_previousPlayerStateWasPaused;
  778. if (self.activityIndicator.isAnimating != shouldAnimate) {
  779. self.activityIndicator.alpha = shouldAnimate ? 1.0 : 0.0;
  780. shouldAnimate ? [self.activityIndicator startAnimating] : [self.activityIndicator stopAnimating];
  781. }
  782. }
  783. - (void)_resetIdleTimer
  784. {
  785. if (!_idleTimer)
  786. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:4.
  787. target:self
  788. selector:@selector(idleTimerExceeded)
  789. userInfo:nil
  790. repeats:NO];
  791. else {
  792. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 4.)
  793. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:4.]];
  794. }
  795. }
  796. - (NSString *)_stringInTimeFormatFrom:(int)duration
  797. {
  798. if (duration < 60) {
  799. return [NSString stringWithFormat:@"%is", duration];
  800. } else {
  801. return [NSString stringWithFormat:@"%im%is", duration / 60, duration % 60];
  802. }
  803. }
  804. - (void)_seekFromTap
  805. {
  806. NSMutableString *hudString = [NSMutableString string];
  807. int seekDuration = (int)_numberOfTapSeek * SHORT_JUMP_DURATION;
  808. if (seekDuration > 0) {
  809. [_vpc jumpForward:SHORT_JUMP_DURATION];
  810. [hudString appendString:@"⇒ "];
  811. _previousJumpState = VLCMovieJumpStateForward;
  812. } else {
  813. [_vpc jumpBackward:SHORT_JUMP_DURATION];
  814. [hudString appendString:@"⇐ "];
  815. _previousJumpState = VLCMovieJumpStateBackward;
  816. }
  817. [hudString appendString: [self _stringInTimeFormatFrom:abs(seekDuration)]];
  818. [self.statusLabel showStatusMessage:[NSString stringWithString:hudString]];
  819. if (_controlsHidden)
  820. [self setControlsHidden:NO animated:NO];
  821. }
  822. - (void)idleTimerExceeded
  823. {
  824. if (![NSThread isMainThread]) {
  825. [self performSelectorOnMainThread:@selector(idleTimerExceeded) withObject:nil waitUntilDone:NO];
  826. return;
  827. }
  828. _idleTimer = nil;
  829. if (!_controlsHidden)
  830. [self toggleControlsVisible];
  831. if (_isTapSeeking) {
  832. _isTapSeeking = NO;
  833. _numberOfTapSeek = 0;
  834. }
  835. if (!_videoFiltersHidden)
  836. _videoFiltersHidden = YES;
  837. if (_equalizerView.hidden == NO)
  838. _equalizerView.hidden = YES;
  839. if (!_playbackSpeedViewHidden)
  840. _playbackSpeedViewHidden = YES;
  841. if (self.scrubIndicatorView.hidden == NO)
  842. self.scrubIndicatorView.hidden = YES;
  843. }
  844. - (UIResponder *)nextResponder
  845. {
  846. [self _resetIdleTimer];
  847. return [super nextResponder];
  848. }
  849. - (VLCDeviceMotion *)deviceMotion
  850. {
  851. if (!_deviceMotion) {
  852. _deviceMotion = [VLCDeviceMotion new];
  853. _deviceMotion.delegate = self;
  854. }
  855. return _deviceMotion;
  856. }
  857. - (void)setupForMediaProjection
  858. {
  859. BOOL mediaHasProjection = [_vpc currentMediaIs360Video];
  860. _fov = mediaHasProjection ? DEFAULT_FOV : 0.f;
  861. [_swipeRecognizerUp setEnabled:!mediaHasProjection];
  862. [_swipeRecognizerDown setEnabled:!mediaHasProjection];
  863. [_swipeRecognizerLeft setEnabled:!mediaHasProjection];
  864. [_swipeRecognizerRight setEnabled:!mediaHasProjection];
  865. if (mediaHasProjection) {
  866. [self.deviceMotion startDeviceMotion];
  867. }
  868. }
  869. - (void)applyYaw:(CGFloat)diffYaw pitch:(CGFloat)diffPitch;
  870. {
  871. //Add and limit new pitch and yaw
  872. self.deviceMotion.yaw += diffYaw;
  873. self.deviceMotion.pitch += diffPitch;
  874. [_vpc updateViewpoint:self.deviceMotion.yaw pitch:self.deviceMotion.pitch roll:0 fov:_fov absolute:YES];
  875. }
  876. - (void)deviceMotionHasAttitudeWithDeviceMotion:(VLCDeviceMotion *)deviceMotion pitch:(double)pitch yaw:(double)yaw
  877. {
  878. if (_panRecognizer.state != UIGestureRecognizerStateChanged || UIGestureRecognizerStateBegan) {
  879. [self applyYaw:yaw pitch:pitch];
  880. }
  881. }
  882. #pragma mark - keyboard controls
  883. - (NSArray<UIKeyCommand *> *)keyCommands
  884. {
  885. NSMutableArray<UIKeyCommand *> *commands = [NSMutableArray arrayWithObjects:
  886. [UIKeyCommand keyCommandWithInput:@" " modifierFlags:0 action:@selector(playPause) discoverabilityTitle:@"Play/Pause"],
  887. [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(playPause) discoverabilityTitle:@"Play/Pause"],
  888. [UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:0 action:@selector(keyBackward) discoverabilityTitle:@"Skip Back"],
  889. [UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:0 action:@selector(keyForward) discoverabilityTitle:@"Skip Forward"],
  890. [UIKeyCommand keyCommandWithInput:@"[" modifierFlags:0 action:@selector(keyLeftBracket) discoverabilityTitle:@"Decrease playback speed"],
  891. [UIKeyCommand keyCommandWithInput:@"]" modifierFlags:0 action:@selector(keyRightBracket) discoverabilityTitle:@"Increase playback speed"],
  892. nil];
  893. bool playbackRateIsOne = ((fabsf(_vpc.playbackRate) - 1) < FLT_EPSILON);
  894. if (playbackRateIsOne) {
  895. UIKeyCommand *resetPlaybackCommand = [UIKeyCommand keyCommandWithInput:@"=" modifierFlags:0 action:@selector(keyEqual) discoverabilityTitle:@"Reset playback speed"];
  896. [commands addObject:resetPlaybackCommand];
  897. }
  898. return commands;
  899. }
  900. - (void)keyForward
  901. {
  902. [_vpc jumpForward:FORWARD_SWIPE_DURATION];
  903. }
  904. - (void)keyBackward
  905. {
  906. [_vpc jumpBackward:BACKWARD_SWIPE_DURATION];
  907. }
  908. - (void)keyEqual
  909. {
  910. [_vpc setPlaybackRate:1];
  911. }
  912. - (void)keyLeftBracket
  913. {
  914. [_vpc setPlaybackRate:_vpc.playbackRate * 0.75];
  915. }
  916. - (void)keyRightBracket
  917. {
  918. [_vpc setPlaybackRate:_vpc.playbackRate * 1.5];
  919. }
  920. #pragma mark - controls
  921. - (void)closePlayback:(id)sender
  922. {
  923. _playbackWillClose = YES;
  924. [_vpc stopPlayback];
  925. }
  926. - (IBAction)minimizePlayback:(id)sender
  927. {
  928. [_delegate movieViewControllerDidSelectMinimize:self];
  929. }
  930. - (IBAction)positionSliderAction:(UISlider *)sender
  931. {
  932. /* we need to limit the number of events sent by the slider, since otherwise, the user
  933. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  934. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  935. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  936. if (_mediaDuration > 0) {
  937. VLCTime *newPosition = [VLCTime timeWithInt:(int)(sender.value * _mediaDuration)];
  938. [self.timeNavigationTitleView.timeDisplayButton setTitle:newPosition.stringValue forState:UIControlStateNormal];
  939. [self.timeNavigationTitleView setNeedsLayout];
  940. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"PLAYBACK_POSITION", nil), newPosition.stringValue];
  941. }
  942. _positionSet = NO;
  943. [self _resetIdleTimer];
  944. }
  945. - (void)_setPositionForReal
  946. {
  947. if (!_positionSet) {
  948. [_vpc setPlaybackPosition:self.timeNavigationTitleView.positionSlider.value];
  949. [_vpc setNeedsMetadataUpdate];
  950. _positionSet = YES;
  951. }
  952. }
  953. - (IBAction)positionSliderTouchDown:(id)sender
  954. {
  955. [self _updateScrubLabel];
  956. self.scrubIndicatorView.hidden = NO;
  957. _isScrubbing = YES;
  958. }
  959. - (IBAction)positionSliderTouchUp:(id)sender
  960. {
  961. self.scrubIndicatorView.hidden = YES;
  962. _isScrubbing = NO;
  963. }
  964. - (void)_updateScrubLabel
  965. {
  966. float speed = self.timeNavigationTitleView.positionSlider.scrubbingSpeed;
  967. if (speed == 1.)
  968. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", nil);
  969. else if (speed == .5)
  970. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", nil);
  971. else if (speed == .25)
  972. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", nil);
  973. else
  974. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", nil);
  975. [self _resetIdleTimer];
  976. }
  977. - (IBAction)positionSliderDrag:(id)sender
  978. {
  979. [self _updateScrubLabel];
  980. }
  981. - (void)volumeSliderAction:(id)sender
  982. {
  983. [self _resetIdleTimer];
  984. }
  985. - (void)updateTimeDisplayButton
  986. {
  987. UIButton *timeDisplayButton = self.timeNavigationTitleView.timeDisplayButton;
  988. if (_displayRemainingTime)
  989. [timeDisplayButton setTitle:[[_vpc remainingTime] stringValue] forState:UIControlStateNormal];
  990. else
  991. [timeDisplayButton setTitle:[[_vpc playedTime] stringValue] forState:UIControlStateNormal];
  992. [self.timeNavigationTitleView setNeedsLayout];
  993. }
  994. #pragma mark - playback controller delegation
  995. - (void)playbackPositionUpdated:(VLCPlaybackController *)controller
  996. {
  997. // FIXME: hard coded state since the state in mediaPlayer is incorrectly still buffering
  998. [self updateActivityIndicatorForState:VLCMediaPlayerStatePlaying];
  999. if (!_isScrubbing) {
  1000. self.timeNavigationTitleView.positionSlider.value = [controller playbackPosition];
  1001. }
  1002. [self updateTimeDisplayButton];
  1003. }
  1004. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  1005. {
  1006. #if !NEW_UI
  1007. self.trackNameLabel.text = @"";
  1008. #else
  1009. [_mediaNavigationBar setMediaTitleLabelText:@""];
  1010. #endif
  1011. self.artistNameLabel.text = self.albumNameLabel.text = @"";
  1012. self.timeNavigationTitleView.positionSlider.value = 0.;
  1013. [self.timeNavigationTitleView.timeDisplayButton setTitle:@"" forState:UIControlStateNormal];
  1014. self.timeNavigationTitleView.timeDisplayButton.accessibilityLabel = @"";
  1015. [_equalizerView reloadData];
  1016. [_playbackSpeedView prepareForMediaPlaybackWithController:controller];
  1017. [self _resetIdleTimer];
  1018. }
  1019. - (void)playbackDidStop:(NSNotification *)notification
  1020. {
  1021. [self minimizePlayback:nil];
  1022. // reset toggleButton to default icon when movieviewcontroller is dismissed
  1023. _videoOptionsControlBar.isInFullScreen = NO;
  1024. }
  1025. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  1026. isPlaying:(BOOL)isPlaying
  1027. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  1028. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  1029. forPlaybackController:(VLCPlaybackController *)controller
  1030. {
  1031. [self updateActivityIndicatorForState:currentState];
  1032. if (currentState == VLCMediaPlayerStateBuffering)
  1033. _mediaDuration = controller.mediaDuration;
  1034. if (currentState == VLCMediaPlayerStateError)
  1035. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", nil)];
  1036. [_controllerPanel updateButtons];
  1037. #if !NEW_UI
  1038. _multiSelectionView.mediaHasChapters = currentMediaHasChapters;
  1039. #endif
  1040. }
  1041. - (void)savePlaybackState:(VLCPlaybackController *)controller
  1042. {
  1043. [_services.medialibraryService savePlaybackStateFrom:controller];
  1044. }
  1045. - (VLCMLMedia *)mediaForPlayingMedia:(VLCMedia *)media
  1046. {
  1047. return [_services.medialibraryService fetchMediaWith:media.url];
  1048. }
  1049. - (void)showStatusMessage:(NSString *)statusMessage
  1050. {
  1051. [self.statusLabel showStatusMessage:statusMessage];
  1052. }
  1053. - (void)hideShowAspectratioButton:(BOOL)hide
  1054. {
  1055. [UIView animateWithDuration:.3
  1056. animations:^{
  1057. self.widthConstraint.constant = hide ? 0 : 30;
  1058. self.timeNavigationTitleView.aspectRatioButton.hidden = hide;
  1059. }];
  1060. }
  1061. - (void) playbackControllerDidSwitchAspectRatio:(NSUInteger)aspectRatio
  1062. {
  1063. _videoOptionsControlBar.isInFullScreen = aspectRatio == VLCAspectRatioFillToScreen;
  1064. }
  1065. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller 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. [[VLCPlaybackController sharedInstance] toggleRepeatMode];
  1274. #if !NEW_UI
  1275. _multiSelectionView.repeatMode = [VLCPlaybackController sharedInstance].repeatMode;
  1276. #else
  1277. _videoOptionsControlBar.repeatMode = [VLCPlaybackController 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. [[VLCPlaybackController 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