VLCMovieViewController.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. VLCMedia *media;
  271. if (self.mediaItem) {
  272. self.title = [self.mediaItem title];
  273. MLFile *item = self.mediaItem;
  274. media = [VLCMedia mediaWithURL:[NSURL URLWithString:item.url]];
  275. item.unread = @(NO);
  276. if (item.isAlbumTrack) {
  277. self.trackNameLabel.text = item.albumTrack.title;
  278. self.artistNameLabel.text = item.albumTrack.artist;
  279. self.albumNameLabel.text = item.albumTrack.album.name;
  280. } else
  281. self.trackNameLabel.text = self.artistNameLabel.text = self.albumNameLabel.text = @"";
  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 popViewControllerAnimated:YES];
  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. - (CGSize)_screenSize
  719. {
  720. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  721. CGRect screenRect = [[UIScreen mainScreen] bounds];
  722. CGFloat screenHeight, screenWidth = .0;
  723. if (orientation == UIDeviceOrientationPortrait) {
  724. screenHeight = screenRect.size.height;
  725. screenWidth = screenRect.size.width;
  726. } else {
  727. screenHeight = screenRect.size.width;
  728. screenWidth = screenRect.size.height;
  729. }
  730. return CGSizeMake(screenWidth, screenHeight);
  731. }
  732. - (void)tapRecognized
  733. {
  734. if ([_mediaPlayer isPlaying]) {
  735. [_mediaPlayer pause];
  736. [self _displayHUDwithText:@" ▌▌"];
  737. } else {
  738. [_mediaPlayer play];
  739. [self _displayHUDwithText:@" ►"];
  740. }
  741. [_rootView addSubview:_splashView];
  742. [UIView animateWithDuration:1
  743. animations:^{ _splashView.alpha = 0.0f;}
  744. completion:^(BOOL finished){ [_splashView removeFromSuperview]; }];
  745. }
  746. - (NSString*)detectPanTypeForPan:(UIPanGestureRecognizer*)panRecognizer
  747. {
  748. NSString * type;
  749. NSString * deviceType = [[UIDevice currentDevice] model];
  750. type = @"Volume"; // default in case of error
  751. CGPoint location = [panRecognizer locationInView:_rootView];
  752. CGFloat position = location.x;
  753. CGSize screenSize = [self _screenSize];
  754. if (position < screenSize.width / 2)
  755. type = @"Brightness";
  756. if (position > screenSize.width / 2)
  757. type = @"Volume";
  758. // only check for seeking gesture if on iPad , will overwrite last statements if true
  759. if ([deviceType isEqualToString:@"iPad"]) {
  760. if (location.y < 110)
  761. type = @"Seek";
  762. }
  763. return type;
  764. }
  765. - (void)_displayHUDwithText:(NSString*)text
  766. {
  767. CGSize screenSize = [self _screenSize];
  768. UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake( (screenSize.width/2 - text.length*8.5/2), 72, (text.length * 8.5), 20)];
  769. [label setText:text];
  770. [label setAdjustsFontSizeToFitWidth:YES];
  771. [label setBackgroundColor:[UIColor blackColor]];
  772. [label setTextColor:[UIColor whiteColor]];
  773. [label setAlpha:0.55];
  774. [_rootView addSubview:label];
  775. [UIView animateWithDuration:1 animations:^{ label.alpha = 0.0f;} completion:^(BOOL finished){ [label removeFromSuperview]; }];
  776. }
  777. - (void)panRecognized:(UIPanGestureRecognizer*)panRecognizer
  778. {
  779. CGFloat panDirectionX = [panRecognizer velocityInView:_rootView].x;
  780. CGFloat panDirectionY = [panRecognizer velocityInView:_rootView].y;
  781. if (panRecognizer.state == UIGestureRecognizerStateBegan) // Only Detect pantype when began to allow more freedom
  782. panType = [self detectPanTypeForPan:panRecognizer];
  783. if ([panType isEqual:@"Seek"]) {
  784. double timeRemainingDouble = (-_mediaPlayer.remainingTime.intValue*0.001);
  785. int timeRemaining = timeRemainingDouble;
  786. if (panDirectionX > 0) {
  787. if (timeRemaining > 2 ) // to not go outside duration , video will stop
  788. [_mediaPlayer jumpForward:1];
  789. } else
  790. [_mediaPlayer jumpBackward:1];
  791. } else if ([panType isEqual:@"Volume"]) {
  792. MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
  793. if (panDirectionY > 0)
  794. musicPlayer.volume -= 0.01;
  795. else
  796. musicPlayer.volume += 0.01;
  797. NSString *volume =[NSString stringWithFormat:@" Volume : %@ %%", [[[NSString stringWithFormat:@"%f",(musicPlayer.volume*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  798. [self _displayHUDwithText:volume];
  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:@" Brightness : %@ %%", [[[NSString stringWithFormat:@"%f",(brightness*100)] componentsSeparatedByString:@"."] objectAtIndex:0]];
  806. [self _displayHUDwithText:brightnessHUD];
  807. }
  808. if (panRecognizer.state == UIGestureRecognizerStateEnded) {
  809. if ([_mediaPlayer isPlaying])
  810. [_mediaPlayer 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. [_rootView addSubview:_splashView];
  833. if ([_mediaPlayer isPlaying])
  834. [_mediaPlayer play];
  835. [self _displayHUDwithText:hudString];
  836. }
  837. }
  838. #pragma mark - Video Filter UI
  839. - (IBAction)videoFilterToggle:(id)sender
  840. {
  841. if (!_playbackSpeedViewHidden)
  842. self.playbackSpeedView.hidden = _playbackSpeedViewHidden = YES;
  843. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  844. if (!_controlsHidden)
  845. self.controllerPanel.hidden = _controlsHidden = YES;
  846. }
  847. self.videoFilterView.hidden = !_videoFiltersHidden;
  848. _videoFiltersHidden = self.videoFilterView.hidden;
  849. }
  850. - (IBAction)videoFilterSliderAction:(id)sender
  851. {
  852. if (sender == self.hueSlider)
  853. _mediaPlayer.hue = (int)self.hueSlider.value;
  854. else if (sender == self.contrastSlider)
  855. _mediaPlayer.contrast = self.contrastSlider.value;
  856. else if (sender == self.brightnessSlider) {
  857. if ([self hasExternalDisplay])
  858. _mediaPlayer.brightness = self.brightnessSlider.value;
  859. else
  860. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  861. } else if (sender == self.saturationSlider)
  862. _mediaPlayer.saturation = self.saturationSlider.value;
  863. else if (sender == self.gammaSlider)
  864. _mediaPlayer.gamma = self.gammaSlider.value;
  865. else if (sender == self.resetVideoFilterButton) {
  866. _mediaPlayer.hue = self.hueSlider.value = 0.;
  867. _mediaPlayer.contrast = self.contrastSlider.value = 1.;
  868. _mediaPlayer.brightness = self.brightnessSlider.value = 1.;
  869. [[UIScreen mainScreen] setBrightness:(self.brightnessSlider.value / 2.)];
  870. _mediaPlayer.saturation = self.saturationSlider.value = 1.;
  871. _mediaPlayer.gamma = self.gammaSlider.value = 1.;
  872. } else
  873. APLog(@"unknown sender for videoFilterSliderAction");
  874. [self _resetIdleTimer];
  875. }
  876. #pragma mark - playback view
  877. - (IBAction)playbackSpeedSliderAction:(UISlider *)sender
  878. {
  879. double speed = pow(2, sender.value / 17.);
  880. float rate = INPUT_RATE_DEFAULT / speed;
  881. if (_currentPlaybackRate != rate)
  882. [_mediaPlayer setRate:INPUT_RATE_DEFAULT / rate];
  883. _currentPlaybackRate = rate;
  884. [self _updatePlaybackSpeedIndicator];
  885. [self _resetIdleTimer];
  886. }
  887. - (void)_updatePlaybackSpeedIndicator
  888. {
  889. float f_value = self.playbackSpeedSlider.value;
  890. double speed = pow(2, f_value / 17.);
  891. self.playbackSpeedIndicator.text = [NSString stringWithFormat:@"%.2fx", speed];
  892. /* rate changed, so update the exported info */
  893. [self performSelectorInBackground:@selector(_updateExportedPlaybackInformation) withObject:nil];
  894. }
  895. - (float)_playbackSpeed
  896. {
  897. float f_rate = _mediaPlayer.rate;
  898. double value = 17 * log(f_rate) / log(2.);
  899. float returnValue = (int) ((value > 0) ? value + .5 : value - .5);
  900. if (returnValue < -34.)
  901. returnValue = -34.;
  902. else if (returnValue > 34.)
  903. returnValue = 34.;
  904. _currentPlaybackRate = returnValue;
  905. return returnValue;
  906. }
  907. - (IBAction)videoDimensionAction:(id)sender
  908. {
  909. if (sender == self.playbackSpeedButton) {
  910. if (!_videoFiltersHidden)
  911. self.videoFilterView.hidden = _videoFiltersHidden = YES;
  912. self.playbackSpeedView.hidden = !_playbackSpeedViewHidden;
  913. _playbackSpeedViewHidden = self.playbackSpeedView.hidden;
  914. [self _resetIdleTimer];
  915. } else if (sender == self.aspectRatioButton) {
  916. NSUInteger count = [_aspectRatios count];
  917. if (_currentAspectRatioMask + 1 > count - 1) {
  918. _mediaPlayer.videoAspectRatio = NULL;
  919. _mediaPlayer.videoCropGeometry = NULL;
  920. _currentAspectRatioMask = 0;
  921. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), NSLocalizedString(@"DEFAULT", @"")]];
  922. } else {
  923. _currentAspectRatioMask++;
  924. if ([_aspectRatios[_currentAspectRatioMask] isEqualToString:@"FILL_TO_SCREEN"]) {
  925. UIScreen *screen;
  926. if (![self hasExternalDisplay])
  927. screen = [UIScreen mainScreen];
  928. else
  929. screen = [UIScreen screens][1];
  930. float f_ar = screen.bounds.size.width / screen.bounds.size.height;
  931. if (f_ar == (float)(640./1136.)) // iPhone 5 aka 16:9.01
  932. _mediaPlayer.videoCropGeometry = "16:9";
  933. else if (f_ar == (float)(2./3.)) // all other iPhones
  934. _mediaPlayer.videoCropGeometry = "16:10"; // libvlc doesn't support 2:3 crop
  935. else if (f_ar == .75) // all iPads
  936. _mediaPlayer.videoCropGeometry = "4:3";
  937. else if (f_ar == .5625) // AirPlay
  938. _mediaPlayer.videoCropGeometry = "16:9";
  939. else
  940. APLog(@"unknown screen format %f, can't crop", f_ar);
  941. [self.statusLabel showStatusMessage:NSLocalizedString(@"FILL_TO_SCREEN", @"")];
  942. return;
  943. }
  944. _mediaPlayer.videoCropGeometry = NULL;
  945. _mediaPlayer.videoAspectRatio = (char *)[_aspectRatios[_currentAspectRatioMask] UTF8String];
  946. [self.statusLabel showStatusMessage:[NSString stringWithFormat:NSLocalizedString(@"AR_CHANGED", @""), _aspectRatios[_currentAspectRatioMask]]];
  947. }
  948. }
  949. }
  950. #pragma mark - background interaction
  951. - (void)applicationWillResignActive:(NSNotification *)aNotification
  952. {
  953. [self _saveCurrentState];
  954. _mediaPlayer.currentVideoTrackIndex = 0;
  955. if (![[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue]) {
  956. if ([_mediaPlayer isPlaying]) {
  957. [_mediaPlayer pause];
  958. _shouldResumePlaying = YES;
  959. }
  960. }
  961. }
  962. - (void)applicationDidEnterBackground:(NSNotification *)notification
  963. {
  964. _shouldResumePlaying = NO;
  965. }
  966. - (void)applicationDidBecomeActive:(NSNotification *)notification
  967. {
  968. _mediaPlayer.currentVideoTrackIndex = 1;
  969. if (_shouldResumePlaying) {
  970. _shouldResumePlaying = NO;
  971. [_mediaPlayer play];
  972. }
  973. }
  974. - (void)_updateExportedPlaybackInformation
  975. {
  976. if (!_mediaItem) {
  977. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
  978. return;
  979. }
  980. MLFile * currentFile = _mediaItem;
  981. /* we omit artwork for now since we had to read it from storage as we can't access
  982. * the artwork cache at the moment - FIXME? */
  983. NSMutableDictionary *currentlyPlayingTrackInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: currentFile.title, MPMediaItemPropertyTitle, @(currentFile.duration.intValue / 1000.), MPMediaItemPropertyPlaybackDuration, @(_mediaPlayer.time.intValue / 1000.), MPNowPlayingInfoPropertyElapsedPlaybackTime, @(_mediaPlayer.rate), MPNowPlayingInfoPropertyPlaybackRate, nil];
  984. if ([currentFile isAlbumTrack]) {
  985. MLAlbumTrack *track = currentFile.albumTrack;
  986. if (track.artist.length > 0)
  987. [currentlyPlayingTrackInfo setObject:track.artist forKey:MPMediaItemPropertyArtist];
  988. if (track.title.length > 0)
  989. [currentlyPlayingTrackInfo setObject:track.title forKey:MPMediaItemPropertyTitle];
  990. if (track.album.name.length > 0)
  991. [currentlyPlayingTrackInfo setObject:track.album.name forKey:MPMediaItemPropertyAlbumTitle];
  992. [currentlyPlayingTrackInfo setObject:[NSNumber numberWithInt:[track.trackNumber intValue]] forKey:MPMediaItemPropertyAlbumTrackNumber];
  993. }
  994. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
  995. }
  996. #pragma mark - autorotation
  997. - (BOOL)shouldAutorotate
  998. {
  999. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  1000. return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  1001. || toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  1002. }
  1003. #pragma mark - AVSession delegate
  1004. - (void)beginInterruption
  1005. {
  1006. if ([[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingContinueAudioInBackgroundKey] boolValue])
  1007. _shouldResumePlaying = YES;
  1008. [_mediaPlayer pause];
  1009. }
  1010. - (void)endInterruption
  1011. {
  1012. if (_shouldResumePlaying) {
  1013. [_mediaPlayer play];
  1014. _shouldResumePlaying = NO;
  1015. }
  1016. }
  1017. #pragma mark - External Display
  1018. - (BOOL)hasExternalDisplay
  1019. {
  1020. return ([[UIScreen screens] count] > 1);
  1021. }
  1022. - (void)showOnExternalDisplay
  1023. {
  1024. UIScreen *screen = [UIScreen screens][1];
  1025. screen.overscanCompensation = UIScreenOverscanCompensationInsetApplicationFrame;
  1026. self.externalWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
  1027. UIViewController *controller = [[VLCExternalDisplayController alloc] init];
  1028. self.externalWindow.rootViewController = controller;
  1029. [controller.view addSubview:_movieView];
  1030. controller.view.frame = screen.bounds;
  1031. _movieView.frame = screen.bounds;
  1032. self.playingExternallyView.hidden = NO;
  1033. self.externalWindow.screen = screen;
  1034. self.externalWindow.hidden = NO;
  1035. }
  1036. - (void)hideFromExternalDisplay
  1037. {
  1038. [self.view addSubview:_movieView];
  1039. [self.view sendSubviewToBack:_movieView];
  1040. _movieView.frame = self.view.frame;
  1041. self.playingExternallyView.hidden = YES;
  1042. self.externalWindow.hidden = YES;
  1043. self.externalWindow = nil;
  1044. }
  1045. - (void)handleExternalScreenDidConnect:(NSNotification *)notification
  1046. {
  1047. [self showOnExternalDisplay];
  1048. }
  1049. - (void)handleExternalScreenDidDisconnect:(NSNotification *)notification
  1050. {
  1051. [self hideFromExternalDisplay];
  1052. }
  1053. @end