VLCMovieViewController.m 48 KB

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