VLCMovieViewController.m 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. //
  2. // VLCMovieViewController.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCMovieViewController.h"
  11. #import "VLCExternalDisplayController.h"
  12. #import <AVFoundation/AVFoundation.h>
  13. #import <CommonCrypto/CommonDigest.h>
  14. #import "UIDevice+SpeedCategory.h"
  15. #import "VLCBugreporter.h"
  16. #import "OBSlider.h"
  17. #import "VLCStatusLabel.h"
  18. #define INPUT_RATE_DEFAULT 1000.
  19. #define FORWARD_SWIPE_DURATION 30
  20. #define BACKWARD_SWIPE_DURATION 10
  21. @interface VLCMovieViewController () <UIGestureRecognizerDelegate, AVAudioSessionDelegate>
  22. {
  23. VLCMediaPlayer *_mediaPlayer;
  24. BOOL _controlsHidden;
  25. BOOL _videoFiltersHidden;
  26. BOOL _playbackSpeedViewHidden;
  27. UIActionSheet *_subtitleActionSheet;
  28. UIActionSheet *_audiotrackActionSheet;
  29. float _currentPlaybackRate;
  30. NSArray *_aspectRatios;
  31. NSUInteger _currentAspectRatioMask;
  32. NSTimer *_idleTimer;
  33. BOOL _shouldResumePlaying;
  34. BOOL _viewAppeared;
  35. BOOL _displayRemainingTime;
  36. BOOL _positionSet;
  37. BOOL _playerIsSetup;
  38. BOOL _isScrubbing;
  39. BOOL _swipeGesturesEnabled;
  40. NSString * panType;
  41. UIView *_rootView;
  42. UIView *_splashView;
  43. UIPanGestureRecognizer *_panRecognizer;
  44. UISwipeGestureRecognizer *_swipeRecognizerLeft;
  45. UISwipeGestureRecognizer *_swipeRecognizerRight;
  46. UITapGestureRecognizer *_tapRecognizer;
  47. }
  48. @property (nonatomic, strong) UIPopoverController *masterPopoverController;
  49. @property (nonatomic, strong) UIWindow *externalWindow;
  50. @end
  51. @implementation VLCMovieViewController
  52. + (void)initialize
  53. {
  54. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  55. NSDictionary *appDefaults = @{kVLCShowRemainingTime : @(YES)};
  56. [defaults registerDefaults:appDefaults];
  57. }
  58. - (void)dealloc
  59. {
  60. if (_splashView)
  61. [_splashView removeFromSuperview];
  62. if (_tapRecognizer)
  63. [_rootView removeGestureRecognizer:_tapRecognizer];
  64. if (_swipeRecognizerLeft)
  65. [_rootView removeGestureRecognizer:_swipeRecognizerLeft];
  66. if (_swipeRecognizerRight)
  67. [_rootView removeGestureRecognizer:_swipeRecognizerRight];
  68. if (_panRecognizer)
  69. [_rootView removeGestureRecognizer:_panRecognizer];
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. }
  72. #pragma mark - Managing the media item
  73. - (void)setMediaItem:(id)newMediaItem
  74. {
  75. if (_mediaItem != newMediaItem) {
  76. [self _stopPlayback];
  77. _mediaItem = newMediaItem;
  78. if (_viewAppeared)
  79. [self _startPlayback];
  80. }
  81. if (self.masterPopoverController != nil)
  82. [self.masterPopoverController dismissPopoverAnimated:YES];
  83. }
  84. - (void)setUrl:(NSURL *)url
  85. {
  86. if (_url != url) {
  87. [self _stopPlayback];
  88. _url = url;
  89. if (_viewAppeared)
  90. [self _startPlayback];
  91. }
  92. }
  93. - (void)viewDidLoad
  94. {
  95. [super viewDidLoad];
  96. self.wantsFullScreenLayout = YES;
  97. self.videoFilterView.hidden = YES;
  98. _videoFiltersHidden = YES;
  99. _hueLabel.text = NSLocalizedString(@"VFILTER_HUE", @"");
  100. _hueSlider.accessibilityLabel = _hueLabel.text;
  101. _hueSlider.isAccessibilityElement = YES;
  102. _contrastLabel.text = NSLocalizedString(@"VFILTER_CONTRAST", @"");
  103. _contrastSlider.accessibilityLabel = _contrastLabel.text;
  104. _contrastSlider.isAccessibilityElement = YES;
  105. _brightnessLabel.text = NSLocalizedString(@"VFILTER_BRIGHTNESS", @"");
  106. _brightnessSlider.accessibilityLabel = _brightnessLabel.text;
  107. _brightnessSlider.isAccessibilityElement = YES;
  108. _saturationLabel.text = NSLocalizedString(@"VFILTER_SATURATION", @"");
  109. _saturationSlider.accessibilityLabel = _saturationLabel.text;
  110. _saturationSlider.isAccessibilityElement = YES;
  111. _gammaLabel.text = NSLocalizedString(@"VFILTER_GAMMA", @"");
  112. _gammaSlider.accessibilityLabel = _gammaLabel.text;
  113. _gammaSlider.isAccessibilityElement = YES;
  114. _playbackSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SPEED", @"");
  115. _playbackSpeedSlider.accessibilityLabel = _playbackSpeedLabel.text;
  116. _playbackSpeedSlider.isAccessibilityElement = YES;
  117. _positionSlider.accessibilityLabel = NSLocalizedString(@"PLAYBACK_POSITION", @"");
  118. _positionSlider.isAccessibilityElement = YES;
  119. _timeDisplay.isAccessibilityElement = YES;
  120. _audioSwitcherButton.accessibilityLabel = NSLocalizedString(@"CHOOSE_AUDIO_TRACK", @"");
  121. _audioSwitcherButton.isAccessibilityElement = YES;
  122. _subtitleSwitcherButton.accessibilityLabel = NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", @"");
  123. _subtitleSwitcherButton.isAccessibilityElement = YES;
  124. _playbackSpeedButton.accessibilityLabel = _playbackSpeedLabel.text;
  125. _playbackSpeedButton.isAccessibilityElement = YES;
  126. _videoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER", @"");
  127. _videoFilterButton.isAccessibilityElement = YES;
  128. _resetVideoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER_RESET_BUTTON", @"");
  129. _resetVideoFilterButton.isAccessibilityElement = YES;
  130. _aspectRatioButton.accessibilityLabel = NSLocalizedString(@"VIDEO_ASPECT_RATIO_BUTTON", @"");
  131. _aspectRatioButton.isAccessibilityElement = YES;
  132. _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", @"");
  133. _playPauseButton.isAccessibilityElement = YES;
  134. _bwdButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", @"");
  135. _bwdButton.isAccessibilityElement = YES;
  136. _fwdButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", @"");
  137. _fwdButton.isAccessibilityElement = YES;
  138. _scrubHelpLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HELP", @"");
  139. self.playbackSpeedView.hidden = YES;
  140. _playbackSpeedViewHidden = YES;
  141. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  142. [center addObserver:self selector:@selector(handleExternalScreenDidConnect:)
  143. name:UIScreenDidConnectNotification object:nil];
  144. [center addObserver:self selector:@selector(handleExternalScreenDidDisconnect:)
  145. name:UIScreenDidDisconnectNotification object:nil];
  146. [center addObserver:self selector:@selector(applicationWillResignActive:)
  147. name:UIApplicationWillResignActiveNotification object:nil];
  148. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  149. name:UIApplicationDidBecomeActiveNotification object:nil];
  150. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  151. name:UIApplicationDidEnterBackgroundNotification object:nil];
  152. _playingExternallyTitle.text = NSLocalizedString(@"PLAYING_EXTERNALLY_TITLE", @"");
  153. _playingExternallyDescription.text = NSLocalizedString(@"PLAYING_EXTERNALLY_DESC", @"");
  154. if ([self hasExternalDisplay])
  155. [self showOnExternalDisplay];
  156. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  157. _movieView.userInteractionEnabled = NO;
  158. UITapGestureRecognizer *tapOnVideoRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleControlsVisible)];
  159. tapOnVideoRecognizer.delegate = self;
  160. [self.view addGestureRecognizer:tapOnVideoRecognizer];
  161. _displayRemainingTime = [[[NSUserDefaults standardUserDefaults] objectForKey:kVLCShowRemainingTime] boolValue];
  162. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  163. pinchRecognizer.delegate = self;
  164. [self.view addGestureRecognizer:pinchRecognizer];
  165. _swipeGesturesEnabled = YES;
  166. if (_swipeGesturesEnabled) {
  167. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  168. [_tapRecognizer setNumberOfTouchesRequired:2];
  169. _panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
  170. [_panRecognizer setMinimumNumberOfTouches:1];
  171. [_panRecognizer setMaximumNumberOfTouches:1];
  172. _swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  173. _swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  174. _swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
  175. _swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
  176. UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
  177. _rootView = window.rootViewController.view;
  178. [_rootView addGestureRecognizer:_swipeRecognizerLeft];
  179. [_rootView addGestureRecognizer:_swipeRecognizerRight];
  180. [_rootView addGestureRecognizer:_panRecognizer];
  181. [_rootView addGestureRecognizer:_tapRecognizer];
  182. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerLeft];
  183. [_panRecognizer requireGestureRecognizerToFail:_swipeRecognizerRight];
  184. _panRecognizer.delegate = self;
  185. _swipeRecognizerRight.delegate = self;
  186. _swipeRecognizerLeft.delegate = self;
  187. _tapRecognizer.delegate = self;
  188. }
  189. _aspectRatios = @[@"DEFAULT", @"FILL_TO_SCREEN", @"4:3", @"16:9", @"16:10", @"2.21:1"];
  190. [self.aspectRatioButton setBackgroundImage:[UIImage imageNamed:@"ratioButton"] forState:UIControlStateNormal];
  191. [self.aspectRatioButton setBackgroundImage:[UIImage imageNamed:@"ratioButtonHighlight"] forState:UIControlStateHighlighted];
  192. [self.aspectRatioButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
  193. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  194. self.backButton.tintColor = [UIColor colorWithRed:(190.0f/255.0f) green:(190.0f/255.0f) blue:(190.0f/255.0f) alpha:1.];
  195. self.toolbar.tintColor = [UIColor whiteColor];
  196. self.toolbar.barTintColor = [UIColor colorWithWhite:0.f alpha:1.f];
  197. CGRect rect = self.positionSlider.frame;
  198. rect.origin.y = rect.origin.y - 5.;
  199. self.positionSlider.frame = rect;
  200. rect = self.resetVideoFilterButton.frame;
  201. rect.origin.y = rect.origin.y + 5.;
  202. self.resetVideoFilterButton.frame = rect;
  203. } else {
  204. [self.toolbar setBackgroundImage:[UIImage imageNamed:@"seekbarBg"] forBarMetrics:UIBarMetricsDefault];
  205. [self.backButton setBackgroundImage:[UIImage imageNamed:@"playbackDoneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  206. [self.backButton setBackgroundImage:[UIImage imageNamed:@"playbackDoneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  207. }
  208. /* this looks a bit weird, but we need to support iOS 5 and should show the same appearance */
  209. UISlider *volumeSlider = nil;
  210. for (id aView in self.volumeView.subviews){
  211. if ([[[aView class] description] isEqualToString:@"MPVolumeSlider"]){
  212. volumeSlider = (UISlider *)aView;
  213. break;
  214. }
  215. }
  216. [volumeSlider setMinimumTrackImage:[[UIImage imageNamed:@"sliderminiValue"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 0)] forState:UIControlStateNormal];
  217. [volumeSlider setMaximumTrackImage:[[UIImage imageNamed:@"slidermaxValue"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 4)] forState:UIControlStateNormal];
  218. [volumeSlider setThumbImage:[UIImage imageNamed:@"volumeballslider"] forState:UIControlStateNormal];
  219. [volumeSlider addTarget:self
  220. action:@selector(volumeSliderAction:)
  221. forControlEvents:UIControlEventValueChanged];
  222. [[AVAudioSession sharedInstance] setDelegate:self];
  223. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  224. self.positionSlider.scrubbingSpeedChangePositions = @[@(0.), @(100.), @(200.), @(300)];
  225. _playerIsSetup = NO;
  226. [self.movieView setAccessibilityLabel:NSLocalizedString(@"VO_VIDEOPLAYER_TITLE", @"")];
  227. [self.movieView setAccessibilityHint:NSLocalizedString(@"VO_VIDEOPLAYER_DOUBLETAP", @"")];
  228. }
  229. - (BOOL)_blobCheck
  230. {
  231. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  232. NSString *directoryPath = searchPaths[0];
  233. if (![[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:@"blob.bin"]])
  234. return NO;
  235. NSData *data = [NSData dataWithContentsOfFile:[directoryPath stringByAppendingPathComponent:@"blob.bin"]];
  236. uint8_t digest[CC_SHA1_DIGEST_LENGTH];
  237. CC_SHA1(data.bytes, data.length, digest);
  238. NSMutableString *hash = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
  239. for (unsigned int u = 0; u < CC_SHA1_DIGEST_LENGTH; u++)
  240. [hash appendFormat:@"%02x", digest[u]];
  241. if ([hash isEqualToString:kBlobHash])
  242. return YES;
  243. else
  244. return NO;
  245. }
  246. - (void)viewWillAppear:(BOOL)animated
  247. {
  248. [super viewWillAppear:animated];
  249. [self.navigationController setNavigationBarHidden:YES animated:YES];
  250. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  251. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  252. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
  253. }
  254. [self _startPlayback];
  255. [self setControlsHidden:NO animated:YES];
  256. _viewAppeared = YES;
  257. }
  258. - (void)_startPlayback
  259. {
  260. if (_playerIsSetup)
  261. return;
  262. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  263. if (!self.mediaItem && !self.url) {
  264. [self _stopPlayback];
  265. return;
  266. }
  267. _mediaPlayer = [[VLCMediaPlayer alloc] initWithOptions:@[[NSString stringWithFormat:@"--%@=%@", kVLCSettingSubtitlesFont, [defaults objectForKey:kVLCSettingSubtitlesFont]], [NSString stringWithFormat:@"--%@=%@", kVLCSettingSubtitlesFontColor, [defaults objectForKey:kVLCSettingSubtitlesFontColor]], [NSString stringWithFormat:@"--%@=%@", kVLCSettingSubtitlesFontSize, [defaults objectForKey:kVLCSettingSubtitlesFontSize]], [NSString stringWithFormat:@"--%@=%@", kVLCSettingDeinterlace, [defaults objectForKey:kVLCSettingDeinterlace]]]];
  268. [_mediaPlayer setDelegate:self];
  269. [_mediaPlayer setDrawable:self.movieView];
  270. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  271. VLCMedia *media;
  272. if (self.mediaItem) {
  273. self.title = [self.mediaItem title];
  274. MLFile *item = self.mediaItem;
  275. media = [VLCMedia mediaWithURL:[NSURL URLWithString:item.url]];
  276. item.unread = @(NO);
  277. if (item.isAlbumTrack) {
  278. self.trackNameLabel.text = item.albumTrack.title;
  279. self.artistNameLabel.text = item.albumTrack.artist;
  280. self.albumNameLabel.text = item.albumTrack.album.name;
  281. }
  282. } else {
  283. media = [VLCMedia mediaWithURL:self.url];
  284. self.title = @"Network Stream";
  285. }
  286. [media addOptions:
  287. @{kVLCSettingStretchAudio :
  288. [[defaults objectForKey:kVLCSettingStretchAudio] boolValue] ? kVLCSettingStretchAudioOnValue : kVLCSettingStretchAudioOffValue, kVLCSettingTextEncoding : [defaults objectForKey:kVLCSettingTextEncoding], kVLCSettingSkipLoopFilter : [defaults objectForKey:kVLCSettingSkipLoopFilter]}];
  289. [NSTimeZone resetSystemTimeZone];
  290. NSString *tzName = [[NSTimeZone systemTimeZone] name];
  291. NSArray *tzNames = @[@"America/Adak", @"America/Anchorage", @"America/Boise", @"America/Chicago", @"America/Denver", @"America/Detroit", @"America/Indiana/Indianapolis", @"America/Indiana/Knox", @"America/Indiana/Marengo", @"America/Indiana/Petersburg", @"America/Indiana/Tell_City", @"America/Indiana/Vevay", @"America/Indiana/Vincennes", @"America/Indiana/Winamac", @"America/Juneau", @"America/Kentucky/Louisville", @"America/Kentucky/Monticello", @"America/Los_Angeles", @"America/Menominee", @"America/Metlakatla", @"America/New_York", @"America/Nome", @"America/North_Dakota/Beulah", @"America/North_Dakota/Center", @"America/North_Dakota/New_Salem", @"America/Phoenix", @"America/Puerto_Rico", @"America/Shiprock", @"America/Sitka", @"America/St_Thomas", @"America/Thule", @"America/Yakutat", @"Pacific/Guam", @"Pacific/Honolulu", @"Pacific/Johnston", @"Pacific/Kwajalein", @"Pacific/Midway", @"Pacific/Pago_Pago", @"Pacific/Saipan", @"Pacific/Wake"];
  292. if ([tzNames containsObject:tzName] || [[tzName stringByDeletingLastPathComponent] isEqualToString:@"US"]) {
  293. NSArray *tracksInfo = media.tracksInformation;
  294. for (NSUInteger x = 0; x < tracksInfo.count; x++) {
  295. if ([[tracksInfo[x] objectForKey:VLCMediaTracksInformationType] isEqualToString:VLCMediaTracksInformationTypeAudio])
  296. {
  297. NSInteger fourcc = [[tracksInfo[x] objectForKey:VLCMediaTracksInformationCodec] integerValue];
  298. switch (fourcc) {
  299. case 540161377:
  300. case 1647457633:
  301. case 858612577:
  302. case 862151027:
  303. case 862151013:
  304. case 1684566644:
  305. case 2126701:
  306. {
  307. if (![self _blobCheck]) {
  308. [media addOptions:@{@"no-audio" : [NSNull null]}];
  309. APLog(@"audio playback disabled because an unsupported codec was found");
  310. }
  311. break;
  312. }
  313. default:
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. [_mediaPlayer setMedia:media];
  320. self.positionSlider.value = 0.;
  321. [self.timeDisplay setTitle:@"" forState:UIControlStateNormal];
  322. self.timeDisplay.accessibilityLabel = @"";
  323. if (![self _isMediaSuitableForDevice]) {
  324. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DEVICE_TOOSLOW_TITLE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DEVICE_TOOSLOW", @""), [[UIDevice currentDevice] model], self.mediaItem.title] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
  325. [alert show];
  326. } else
  327. [self _playNewMedia];
  328. if (![self hasExternalDisplay])
  329. self.brightnessSlider.value = [UIScreen mainScreen].brightness * 2.;
  330. }
  331. - (BOOL)_isMediaSuitableForDevice
  332. {
  333. if (!self.mediaItem)
  334. return YES;
  335. NSUInteger totalNumberOfPixels = [[[self.mediaItem videoTrack] valueForKey:@"width"] doubleValue] * [[[self.mediaItem videoTrack] valueForKey:@"height"] doubleValue];
  336. NSInteger speedCategory = [[UIDevice currentDevice] speedCategory];
  337. if (speedCategory == 1) {
  338. // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
  339. return (totalNumberOfPixels < 600000); // between 480p and 720p
  340. } else if (speedCategory == 2) {
  341. // iPhone 4S, iPad 2 and 3, iPod 4 and 5
  342. return (totalNumberOfPixels < 922000); // 720p
  343. } else if (speedCategory == 3) {
  344. // iPhone 5, iPad 4
  345. return (totalNumberOfPixels < 2074000); // 1080p
  346. }
  347. return YES;
  348. }
  349. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  350. {
  351. if (buttonIndex == 1)
  352. [self _playNewMedia];
  353. else {
  354. [self _stopPlayback];
  355. [self closePlayback:nil];
  356. }
  357. }
  358. - (void)_playNewMedia
  359. {
  360. NSNumber *playbackPositionInTime = @(0);
  361. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < .95) {
  362. if (self.mediaItem.duration.intValue != 0)
  363. playbackPositionInTime = @(self.mediaItem.lastPosition.floatValue * (self.mediaItem.duration.intValue / 1000.));
  364. }
  365. if (playbackPositionInTime.intValue > 0) {
  366. [_mediaPlayer.media addOptions:@{@"start-time": playbackPositionInTime}];
  367. APLog(@"set starttime to %i", playbackPositionInTime.intValue);
  368. }
  369. [_mediaPlayer addObserver:self forKeyPath:@"time" options:0 context:nil];
  370. [_mediaPlayer addObserver:self forKeyPath:@"remainingTime" options:0 context:nil];
  371. [_mediaPlayer play];
  372. if (self.mediaItem) {
  373. if (self.mediaItem.lastAudioTrack.intValue > 0)
  374. _mediaPlayer.currentAudioTrackIndex = self.mediaItem.lastAudioTrack.intValue;
  375. if (self.mediaItem.lastSubtitleTrack.intValue > 0)
  376. _mediaPlayer.currentVideoSubTitleIndex = self.mediaItem.lastSubtitleTrack.intValue;
  377. }
  378. self.playbackSpeedSlider.value = [self _playbackSpeed];
  379. [self _updatePlaybackSpeedIndicator];
  380. [self performSelectorInBackground:@selector(_updateExportedPlaybackInformation) withObject:nil];
  381. _currentAspectRatioMask = 0;
  382. _mediaPlayer.videoAspectRatio = NULL;
  383. [self _resetIdleTimer];
  384. _playerIsSetup = YES;
  385. }
  386. - (void)viewWillDisappear:(BOOL)animated
  387. {
  388. [self _stopPlayback];
  389. _viewAppeared = NO;
  390. if (_idleTimer) {
  391. [_idleTimer invalidate];
  392. _idleTimer = nil;
  393. }
  394. [self.navigationController setNavigationBarHidden:NO animated:YES];
  395. if (!SYSTEM_RUNS_IOS7_OR_LATER)
  396. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  397. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  398. [super viewWillDisappear:animated];
  399. // hide filter UI for next run
  400. if (!_videoFiltersHidden)
  401. _videoFiltersHidden = YES;
  402. if (!_playbackSpeedViewHidden)
  403. _playbackSpeedViewHidden = YES;
  404. }
  405. - (void)_stopPlayback
  406. {
  407. if (_mediaPlayer) {
  408. @try {
  409. [_mediaPlayer removeObserver:self forKeyPath:@"time"];
  410. [_mediaPlayer removeObserver:self forKeyPath:@"remainingTime"];
  411. }
  412. @catch (NSException *exception) {
  413. APLog(@"we weren't an observer yet");
  414. }
  415. if (_mediaPlayer.media) {
  416. [_mediaPlayer pause];
  417. [self _saveCurrentState];
  418. [_mediaPlayer stop];
  419. }
  420. if (_mediaPlayer)
  421. _mediaPlayer = nil;
  422. }
  423. if (_mediaItem)
  424. _mediaItem = nil;
  425. _playerIsSetup = NO;
  426. }
  427. - (void)_saveCurrentState
  428. {
  429. if (self.mediaItem) {
  430. @try {
  431. MLFile *item = self.mediaItem;
  432. item.lastPosition = @([_mediaPlayer position]);
  433. item.lastAudioTrack = @(_mediaPlayer.currentAudioTrackIndex);
  434. item.lastSubtitleTrack = @(_mediaPlayer.currentVideoSubTitleIndex);
  435. }
  436. @catch (NSException *exception) {
  437. APLog(@"failed to save current media state - file removed?");
  438. }
  439. }
  440. }
  441. #pragma mark - remote events
  442. - (void)viewDidAppear:(BOOL)animated
  443. {
  444. [super viewDidAppear:animated];
  445. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  446. [self becomeFirstResponder];
  447. }
  448. - (void)viewDidDisappear:(BOOL)animated
  449. {
  450. [super viewDidDisappear:animated];
  451. [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
  452. [self resignFirstResponder];
  453. [[NSUserDefaults standardUserDefaults] setBool:_displayRemainingTime forKey:kVLCShowRemainingTime];
  454. }
  455. - (BOOL)canBecomeFirstResponder
  456. {
  457. return YES;
  458. }
  459. - (void)remoteControlReceivedWithEvent:(UIEvent *)event
  460. {
  461. switch (event.subtype) {
  462. case UIEventSubtypeRemoteControlPlay:
  463. [_mediaPlayer play];
  464. break;
  465. case UIEventSubtypeRemoteControlPause:
  466. [_mediaPlayer pause];
  467. break;
  468. case UIEventSubtypeRemoteControlTogglePlayPause:
  469. [self playPause];
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. #pragma mark - controls visibility
  476. - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
  477. {
  478. if (recognizer.velocity < 0.)
  479. [self closePlayback:nil];
  480. }
  481. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  482. {
  483. if (touch.view != self.view)
  484. return NO;
  485. return YES;
  486. }
  487. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  488. {
  489. return YES;
  490. }
  491. - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated
  492. {
  493. _controlsHidden = hidden;
  494. CGFloat alpha = _controlsHidden? 0.0f: 1.0f;
  495. if (!_controlsHidden) {
  496. _controllerPanel.alpha = 0.0f;
  497. _controllerPanel.hidden = !_videoFiltersHidden;
  498. _toolbar.alpha = 0.0f;
  499. _toolbar.hidden = NO;
  500. _videoFilterView.alpha = 0.0f;
  501. _videoFilterView.hidden = _videoFiltersHidden;
  502. _playbackSpeedView.alpha = 0.0f;
  503. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  504. }
  505. void (^animationBlock)() = ^() {
  506. _controllerPanel.alpha = alpha;
  507. _toolbar.alpha = alpha;
  508. _videoFilterView.alpha = alpha;
  509. _playbackSpeedView.alpha = alpha;
  510. };
  511. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  512. if (_videoFiltersHidden)
  513. _controllerPanel.hidden = _controlsHidden;
  514. else
  515. _controllerPanel.hidden = NO;
  516. _toolbar.hidden = _controlsHidden;
  517. _videoFilterView.hidden = _videoFiltersHidden;
  518. _playbackSpeedView.hidden = _playbackSpeedViewHidden;
  519. };
  520. UIStatusBarAnimation animationType = animated? UIStatusBarAnimationFade: UIStatusBarAnimationNone;
  521. NSTimeInterval animationDuration = animated? 0.3: 0.0;
  522. [[UIApplication sharedApplication] setStatusBarHidden:_viewAppeared ? _controlsHidden : NO withAnimation:animationType];
  523. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  524. _volumeView.hidden = _controllerPanel.hidden;
  525. }
  526. - (void)toggleControlsVisible
  527. {
  528. if (_controlsHidden && !_videoFiltersHidden)
  529. _videoFiltersHidden = YES;
  530. [self setControlsHidden:!_controlsHidden animated:YES];
  531. }
  532. - (void)_resetIdleTimer
  533. {
  534. if (!_idleTimer)
  535. _idleTimer = [NSTimer scheduledTimerWithTimeInterval:4.
  536. target:self
  537. selector:@selector(idleTimerExceeded)
  538. userInfo:nil
  539. repeats:NO];
  540. else {
  541. if (fabs([_idleTimer.fireDate timeIntervalSinceNow]) < 4.)
  542. [_idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:4.]];
  543. }
  544. }
  545. - (void)idleTimerExceeded
  546. {
  547. _idleTimer = nil;
  548. if (!_controlsHidden)
  549. [self toggleControlsVisible];
  550. if (!_videoFiltersHidden)
  551. _videoFiltersHidden = YES;
  552. if (!_playbackSpeedViewHidden)
  553. _playbackSpeedViewHidden = YES;
  554. if (self.scrubIndicatorView.hidden == NO)
  555. self.scrubIndicatorView.hidden = YES;
  556. }
  557. - (UIResponder *)nextResponder
  558. {
  559. [self _resetIdleTimer];
  560. return [super nextResponder];
  561. }
  562. #pragma mark - controls
  563. - (IBAction)closePlayback:(id)sender
  564. {
  565. [self setControlsHidden:NO animated:NO];
  566. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  567. }
  568. - (IBAction)positionSliderAction:(UISlider *)sender
  569. {
  570. /* we need to limit the number of events sent by the slider, since otherwise, the user
  571. * wouldn't see the I-frames when seeking on current mobile devices. This isn't a problem
  572. * within the Simulator, but especially on older ARMv7 devices, it's clearly noticeable. */
  573. [self performSelector:@selector(_setPositionForReal) withObject:nil afterDelay:0.3];
  574. VLCTime *newPosition = [VLCTime timeWithInt:(int)(_positionSlider.value * self.mediaItem.duration.intValue)];
  575. [self.timeDisplay setTitle:newPosition.stringValue forState:UIControlStateNormal];
  576. self.timeDisplay.accessibilityLabel = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"PLAYBACK_POSITION", @""), newPosition.stringValue];
  577. _positionSet = NO;
  578. [self _resetIdleTimer];
  579. }
  580. - (void)_setPositionForReal
  581. {
  582. if (!_positionSet) {
  583. _mediaPlayer.position = _positionSlider.value;
  584. _positionSet = YES;
  585. }
  586. }
  587. - (IBAction)positionSliderTouchDown:(id)sender
  588. {
  589. [self _updateScrubLabel];
  590. self.scrubIndicatorView.hidden = NO;
  591. _isScrubbing = YES;
  592. }
  593. - (IBAction)positionSliderTouchUp:(id)sender
  594. {
  595. self.scrubIndicatorView.hidden = YES;
  596. _isScrubbing = NO;
  597. }
  598. - (void)_updateScrubLabel
  599. {
  600. float speed = self.positionSlider.scrubbingSpeed;
  601. if (speed == 1.)
  602. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HIGH", @"");
  603. else if (speed == .5)
  604. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_HALF", @"");
  605. else if (speed == .25)
  606. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_QUARTER", @"");
  607. else
  608. self.currentScrubSpeedLabel.text = NSLocalizedString(@"PLAYBACK_SCRUB_FINE", @"");
  609. [self _resetIdleTimer];
  610. }
  611. - (IBAction)positionSliderDrag:(id)sender
  612. {
  613. [self _updateScrubLabel];
  614. }
  615. - (IBAction)volumeSliderAction:(id)sender
  616. {
  617. [self _resetIdleTimer];
  618. }
  619. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  620. {
  621. if (!_isScrubbing) {
  622. self.positionSlider.value = [_mediaPlayer position];
  623. }
  624. if (_displayRemainingTime)
  625. [self.timeDisplay setTitle:[[_mediaPlayer remainingTime] stringValue] forState:UIControlStateNormal];
  626. else
  627. [self.timeDisplay setTitle:[[_mediaPlayer time] stringValue] forState:UIControlStateNormal];
  628. }
  629. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  630. {
  631. VLCMediaPlayerState currentState = _mediaPlayer.state;
  632. if (currentState == VLCMediaPlayerStateError) {
  633. [self.statusLabel showStatusMessage:NSLocalizedString(@"PLAYBACK_FAILED", @"")];
  634. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  635. }
  636. if (currentState == VLCMediaPlayerStateEnded || currentState == VLCMediaPlayerStateStopped)
  637. [self performSelector:@selector(closePlayback:) withObject:nil afterDelay:2.];
  638. UIImage *playPauseImage = [_mediaPlayer isPlaying]? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  639. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  640. if ([[_mediaPlayer audioTrackIndexes] count] > 2)
  641. self.audioSwitcherButton.hidden = NO;
  642. else
  643. self.audioSwitcherButton.hidden = YES;
  644. if ([[_mediaPlayer videoSubTitlesIndexes] count] > 1)
  645. self.subtitleSwitcherButton.hidden = NO;
  646. else
  647. self.subtitleSwitcherButton.hidden = YES;
  648. }
  649. - (IBAction)playPause
  650. {
  651. if ([_mediaPlayer isPlaying])
  652. [_mediaPlayer pause];
  653. else
  654. [_mediaPlayer play];
  655. }
  656. - (IBAction)forward:(id)sender
  657. {
  658. [_mediaPlayer mediumJumpForward];
  659. }
  660. - (IBAction)backward:(id)sender
  661. {
  662. [_mediaPlayer mediumJumpBackward];
  663. }
  664. - (IBAction)switchAudioTrack:(id)sender
  665. {
  666. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_AUDIO_TRACK", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  667. NSArray *audioTracks = [_mediaPlayer audioTrackNames];
  668. NSArray *audioTrackIndexes = [_mediaPlayer audioTrackIndexes];
  669. NSUInteger count = [audioTracks count];
  670. for (NSUInteger i = 0; i < count; i++) {
  671. NSString *indexIndicator = ([audioTrackIndexes[i] intValue] == [_mediaPlayer currentAudioTrackIndex])? @"\u2713": @"";
  672. NSString *buttonTitle = [NSString stringWithFormat:@"%@ %@", indexIndicator, audioTracks[i]];
  673. [_audiotrackActionSheet addButtonWithTitle:buttonTitle];
  674. }
  675. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  676. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  677. [_audiotrackActionSheet showInView:self.audioSwitcherButton];
  678. }
  679. - (IBAction)switchSubtitleTrack:(id)sender
  680. {
  681. NSArray *spuTracks = [_mediaPlayer videoSubTitlesNames];
  682. NSArray *spuTrackIndexes = [_mediaPlayer videoSubTitlesIndexes];
  683. NSUInteger count = [spuTracks count];
  684. if (count <= 1)
  685. return;
  686. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  687. for (NSUInteger i = 0; i < count; i++) {
  688. NSString *indexIndicator = ([spuTrackIndexes[i] intValue] == [_mediaPlayer currentVideoSubTitleIndex])? @"\u2713": @"";
  689. NSString *buttonTitle = [NSString stringWithFormat:@"%@ %@", indexIndicator, spuTracks[i]];
  690. [_subtitleActionSheet addButtonWithTitle:buttonTitle];
  691. }
  692. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", @"cancel button")];
  693. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  694. [_subtitleActionSheet showInView: self.subtitleSwitcherButton];
  695. }
  696. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  697. if (buttonIndex == [actionSheet cancelButtonIndex])
  698. return;
  699. NSArray *indexArray;
  700. if (actionSheet == _subtitleActionSheet) {
  701. indexArray = _mediaPlayer.videoSubTitlesIndexes;
  702. if (buttonIndex <= indexArray.count) {
  703. _mediaPlayer.currentVideoSubTitleIndex = [indexArray[buttonIndex] intValue];
  704. }
  705. } else if (actionSheet == _audiotrackActionSheet) {
  706. indexArray = _mediaPlayer.audioTrackIndexes;
  707. if (buttonIndex <= indexArray.count) {
  708. _mediaPlayer.currentAudioTrackIndex = [indexArray[buttonIndex] intValue];
  709. }
  710. }
  711. }
  712. - (IBAction)toggleTimeDisplay:(id)sender
  713. {
  714. _displayRemainingTime = !_displayRemainingTime;
  715. [self _resetIdleTimer];
  716. }
  717. #pragma mark - multi-touch gestures
  718. - (void)tapRecognized
  719. {
  720. if ([_mediaPlayer isPlaying]) {
  721. [_mediaPlayer pause];
  722. [self.statusLabel showStatusMessage:@" ▌▌"];
  723. } else {
  724. [_mediaPlayer play];
  725. [self.statusLabel showStatusMessage:@" ►"];
  726. }
  727. [_rootView addSubview:_splashView];
  728. [UIView animateWithDuration:1
  729. animations:^{ _splashView.alpha = 0.0f;}
  730. completion:^(BOOL finished){ [_splashView removeFromSuperview]; }];
  731. }
  732. - (NSString*)detectPanTypeForPan:(UIPanGestureRecognizer*)panRecognizer
  733. {
  734. NSString * type;
  735. NSString * deviceType = [[UIDevice currentDevice] model];
  736. type = @"Volume"; // default in case of error
  737. CGPoint location = [panRecognizer locationInView:_rootView];
  738. CGFloat position = location.x;
  739. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  740. CGRect screenRect = [[UIScreen mainScreen] bounds];
  741. CGFloat screenWidth = .0;
  742. if (orientation == UIDeviceOrientationPortrait)
  743. screenWidth = screenRect.size.width;
  744. else
  745. screenWidth = screenRect.size.height;
  746. if (position < screenWidth / 2)
  747. type = @"Brightness";
  748. if (position > screenWidth / 2)
  749. type = @"Volume";
  750. // only check for seeking gesture if on iPad , will overwrite last statements if true
  751. if ([deviceType isEqualToString:@"iPad"]) {
  752. if (location.y < 110)
  753. type = @"Seek";
  754. }
  755. return type;
  756. }
  757. - (void)panRecognized:(UIPanGestureRecognizer*)panRecognizer
  758. {
  759. CGFloat panDirectionX = [panRecognizer velocityInView:_rootView].x;
  760. CGFloat panDirectionY = [panRecognizer velocityInView:_rootView].y;
  761. if (panRecognizer.state == UIGestureRecognizerStateBegan) // Only Detect pantype when began to allow more freedom
  762. panType = [self detectPanTypeForPan:panRecognizer];
  763. if ([panType isEqual:@"Seek"]) {
  764. double timeRemainingDouble = (-_mediaPlayer.remainingTime.intValue*0.001);
  765. int timeRemaining = timeRemainingDouble;
  766. if (panDirectionX > 0) {
  767. if (timeRemaining > 2 ) // to not go outside duration , video will stop
  768. [_mediaPlayer jumpForward:1];
  769. } else
  770. [_mediaPlayer jumpBackward:1];
  771. } else if ([panType isEqual:@"Volume"]) {
  772. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
  773. if (panDirectionY > 0)
  774. musicPlayer.volume -= 0.01;
  775. else
  776. musicPlayer.volume += 0.01;
  777. } else if ([panType isEqual:@"Brightness"]) {
  778. CGFloat brightness = [UIScreen mainScreen].brightness;
  779. if (panDirectionY > 0)
  780. [[UIScreen mainScreen] setBrightness:(brightness - 0.01)];
  781. else
  782. [[UIScreen mainScreen] setBrightness:(brightness + 0.01)];
  783. NSString *brightnessHUD = [NSString stringWithFormat:@"%@: %@ %%", NSLocalizedString(@"VFILTER_BRIGHTNESS", @""), [[[NSString stringWithFormat:@"%f",(brightness*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  784. [self.statusLabel showStatusMessage:brightnessHUD];
  785. }
  786. if (panRecognizer.state == UIGestureRecognizerStateEnded) {
  787. if ([_mediaPlayer isPlaying])
  788. [_mediaPlayer play];
  789. }
  790. }
  791. - (void)swipeRecognized:(UISwipeGestureRecognizer*)swipeRecognizer
  792. {
  793. NSString * hudString = @" ";
  794. if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
  795. double timeRemainingDouble = (-_mediaPlayer.remainingTime.intValue*0.001);
  796. int timeRemaining = timeRemainingDouble;
  797. if (FORWARD_SWIPE_DURATION < timeRemaining) {
  798. [_mediaPlayer jumpForward:FORWARD_SWIPE_DURATION];
  799. hudString = [NSString stringWithFormat:@"⇒ %is", FORWARD_SWIPE_DURATION];
  800. } else {
  801. [_mediaPlayer jumpForward:(timeRemaining - 5)];
  802. hudString = [NSString stringWithFormat:@"⇒ %is",(timeRemaining - 5)];
  803. }
  804. }
  805. else if (swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
  806. [_mediaPlayer jumpBackward:BACKWARD_SWIPE_DURATION];
  807. hudString = [NSString stringWithFormat:@"⇐ %is",BACKWARD_SWIPE_DURATION];
  808. }
  809. if (swipeRecognizer.state == UIGestureRecognizerStateEnded) {
  810. [_rootView addSubview:_splashView];
  811. if ([_mediaPlayer isPlaying])
  812. [_mediaPlayer play];
  813. [self.statusLabel showStatusMessage:hudString];
  814. }
  815. }
  816. #pragma mark - Video Filter UI
  817. - (IBAction)videoFilterToggle:(id)sender
  818. {
  819. if (!_playbackSpeedViewHidden)
  820. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  821. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  822. if (!_controlsHidden)
  823. self.controllerPanel.hidden = _controlsHidden = YES;
  824. }
  825. self.videoFilterView.hidden = !_videoFiltersHidden;
  826. _videoFiltersHidden = self.videoFilterView.hidden;
  827. }
  828. - (IBAction)videoFilterSliderAction:(id)sender
  829. {
  830. if (sender == self.hueSlider)
  831. _mediaPlayer.hue = (int)self.hueSlider.value;
  832. else if (sender == self.contrastSlider)
  833. _mediaPlayer.contrast = self.contrastSlider.value;
  834. else if (sender == self.brightnessSlider) {
  835. if ([self hasExternalDisplay])
  836. _mediaPlayer.brightness = self.brightnessSlider.value;
  837. else
  838. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  839. } else if (sender == self.saturationSlider)
  840. _mediaPlayer.saturation = self.saturationSlider.value;
  841. else if (sender == self.gammaSlider)
  842. _mediaPlayer.gamma = self.gammaSlider.value;
  843. else if (sender == self.resetVideoFilterButton) {
  844. _mediaPlayer.hue = self.hueSlider.value = 0.;
  845. _mediaPlayer.contrast = self.contrastSlider.value = 1.;
  846. _mediaPlayer.brightness = self.brightnessSlider.value = 1.;
  847. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  848. _mediaPlayer.saturation = self.saturationSlider.value = 1.;
  849. _mediaPlayer.gamma = self.gammaSlider.value = 1.;
  850. } else
  851. APLog(@"unknown sender for videoFilterSliderAction");
  852. [self _resetIdleTimer];
  853. }
  854. #pragma mark - playback view
  855. - (IBAction)playbackSpeedSliderAction:(UISlider *)sender
  856. {
  857. double speed = pow(2, sender.value / 17.);
  858. float rate = INPUT_RATE_DEFAULT / speed;
  859. if (_currentPlaybackRate != rate)
  860. [_mediaPlayer setRate:INPUT_RATE_DEFAULT / rate];
  861. _currentPlaybackRate = rate;
  862. [self _updatePlaybackSpeedIndicator];
  863. [self _resetIdleTimer];
  864. }
  865. - (void)_updatePlaybackSpeedIndicator
  866. {
  867. float f_value = self.playbackSpeedSlider.value;
  868. double speed = pow(2, f_value / 17.);
  869. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  870. /* rate changed, so update the exported info */
  871. [self performSelectorInBackground:@selector(_updateExportedPlaybackInformation) withObject:nil];
  872. }
  873. - (float)_playbackSpeed
  874. {
  875. float f_rate = _mediaPlayer.rate;
  876. double value = 17 * log(f_rate) / log(2.);
  877. float returnValue = (int) ((value > 0) ? value + .5 : value - .5);
  878. if (returnValue < -34.)
  879. returnValue = -34.;
  880. else if (returnValue > 34.)
  881. returnValue = 34.;
  882. _currentPlaybackRate = returnValue;
  883. return returnValue;
  884. }
  885. - (IBAction)videoDimensionAction:(id)sender
  886. {
  887. if (sender == self.playbackSpeedButton) {
  888. if (!_videoFiltersHidden)
  889. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  890. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  891. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  892. [self _resetIdleTimer];
  893. } else if (sender == self.aspectRatioButton) {
  894. NSUInteger count = [_aspectRatios count];
  895. if (_currentAspectRatioMask + 1 > count - 1) {
  896. _mediaPlayer.videoAspectRatio = NULL;
  897. _mediaPlayer.videoCropGeometry = NULL;
  898. _currentAspectRatioMask = 0;
  899. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), NSLocalizedString(@"DEFAULT", @"")]];
  900. } else {
  901. _currentAspectRatioMask++;
  902. if ([_aspectRatios[_currentAspectRatioMask] isEqualToString:@"FILL_TO_SCREEN"]) {
  903. UIScreen *screen;
  904. if (![self hasExternalDisplay])
  905. screen = [UIScreen mainScreen];
  906. else
  907. screen = [UIScreen screens][1];
  908. float f_ar = screen.bounds.size.width / screen.bounds.size.height;
  909. if (f_ar == (float)(640./1136.)) // iPhone 5 aka 16:9.01
  910. _mediaPlayer.videoCropGeometry = "16:9";
  911. else if (f_ar == (float)(2./3.)) // all other iPhones
  912. _mediaPlayer.videoCropGeometry = "16:10"; // libvlc doesn't support 2:3 crop
  913. else if (f_ar == .75) // all iPads
  914. _mediaPlayer.videoCropGeometry = "4:3";
  915. else if (f_ar == .5625) // AirPlay
  916. _mediaPlayer.videoCropGeometry = "16:9";
  917. else
  918. APLog(@"unknown screen format %f, can't crop", f_ar);
  919. [self.statusLabel showStatusMessage:NSLocalizedString(@"FILL_TO_SCREEN", @"")];
  920. return;
  921. }
  922. _mediaPlayer.videoCropGeometry = NULL;
  923. _mediaPlayer.videoAspectRatio = (char *)[_aspectRatios[_currentAspectRatioMask] UTF8String];
  924. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), _aspectRatios[_currentAspectRatioMask]]];
  925. }
  926. }
  927. }
  928. #pragma mark - background interaction
  929. - (void)applicationWillResignActive:(NSNotification *)aNotification
  930. {
  931. [self _saveCurrentState];
  932. _mediaPlayer.currentVideoTrackIndex = 0;
  933. if (![[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue]) {
  934. if ([_mediaPlayer isPlaying]) {
  935. [_mediaPlayer pause];
  936. _shouldResumePlaying = YES;
  937. }
  938. }
  939. }
  940. - (void)applicationDidEnterBackground:(NSNotification *)notification
  941. {
  942. _shouldResumePlaying = NO;
  943. }
  944. - (void)applicationDidBecomeActive:(NSNotification *)notification
  945. {
  946. _mediaPlayer.currentVideoTrackIndex = 1;
  947. if (_shouldResumePlaying) {
  948. _shouldResumePlaying = NO;
  949. [_mediaPlayer play];
  950. }
  951. }
  952. - (void)_updateExportedPlaybackInformation
  953. {
  954. if (!_mediaItem) {
  955. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
  956. return;
  957. }
  958. MLFile * currentFile = _mediaItem;
  959. /* we omit artwork for now since we had to read it from storage as we can't access
  960. * the artwork cache at the moment - FIXME? */
  961. NSMutableDictionary *currentlyPlayingTrackInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: currentFile.title, MPMediaItemPropertyTitle, @(currentFile.duration.intValue / 1000.), MPMediaItemPropertyPlaybackDuration, @(_mediaPlayer.time.intValue / 1000.), MPNowPlayingInfoPropertyElapsedPlaybackTime, @(_mediaPlayer.rate), MPNowPlayingInfoPropertyPlaybackRate, nil];
  962. if ([currentFile isAlbumTrack]) {
  963. MLAlbumTrack *track = currentFile.albumTrack;
  964. if (track.artist.length > 0)
  965. [currentlyPlayingTrackInfo setObject:track.artist forKey:MPMediaItemPropertyArtist];
  966. if (track.title.length > 0)
  967. [currentlyPlayingTrackInfo setObject:track.title forKey:MPMediaItemPropertyTitle];
  968. if (track.album.name.length > 0)
  969. [currentlyPlayingTrackInfo setObject:track.album.name forKey:MPMediaItemPropertyAlbumTitle];
  970. [currentlyPlayingTrackInfo setObject:[NSNumber numberWithInt:[track.trackNumber intValue]] forKey:MPMediaItemPropertyAlbumTrackNumber];
  971. }
  972. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
  973. }
  974. #pragma mark - autorotation
  975. - (BOOL)shouldAutorotate
  976. {
  977. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  978. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  979. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  980. }
  981. #pragma mark - AVSession delegate
  982. - (void)beginInterruption
  983. {
  984. if ([[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue])
  985. _shouldResumePlaying = YES;
  986. [_mediaPlayer pause];
  987. }
  988. - (void)endInterruption
  989. {
  990. if (_shouldResumePlaying) {
  991. [_mediaPlayer play];
  992. _shouldResumePlaying = NO;
  993. }
  994. }
  995. #pragma mark - External Display
  996. - (BOOL)hasExternalDisplay
  997. {
  998. return ([[UIScreen screens] count] > 1);
  999. }
  1000. - (void)showOnExternalDisplay
  1001. {
  1002. UIScreen *screen = [UIScreen screens][1];
  1003. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  1004. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  1005. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  1006. self.externalWindow.rootViewController = controller;
  1007. [controller.view addSubview:_movieView];
  1008. controller.view.frame = screen.bounds;
  1009. _movieView.frame = screen.bounds;
  1010. self.playingExternallyView.hidden = NO;
  1011. self.externalWindow.screen = screen;
  1012. self.externalWindow.hidden = NO;
  1013. }
  1014. - (void)hideFromExternalDisplay
  1015. {
  1016. [self.view addSubview:_movieView];
  1017. [self.view sendSubviewToBack:_movieView];
  1018. _movieView.frame = self.view.frame;
  1019. self.playingExternallyView.hidden = YES;
  1020. self.externalWindow.hidden = YES;
  1021. self.externalWindow = nil;
  1022. }
  1023. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  1024. {
  1025. [self showOnExternalDisplay];
  1026. }
  1027. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  1028. {
  1029. [self hideFromExternalDisplay];
  1030. }
  1031. @end