VLCMovieViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #import "VLCMovieViewController.h"
  9. @interface VLCMovieViewController ()
  10. @property (strong, nonatomic) UIPopoverController *masterPopoverController;
  11. @end
  12. @implementation VLCMovieViewController
  13. @synthesize movieView=_movieView, tapBarView=_tapBarView, backButton=_backButton, positionSlider=_positionSlider, timeDisplay=_timeDisplay, playPauseButton = _playPauseButton, bwdButton = _bwdButton, fwdButton = _fwdButton, subtitleSwitcherButton = _subtitleSwitcherButton, audioSwitcherButton = _audioSwitcherButton, controllerPanel = _controllerPanel;
  14. - (void)dealloc
  15. {
  16. [_mediaItem release];
  17. [_masterPopoverController release];
  18. [super dealloc];
  19. }
  20. #pragma mark - Managing the media item
  21. - (void)setMediaItem:(id)newMediaItem
  22. {
  23. if (_mediaItem != newMediaItem) {
  24. [_mediaItem release];
  25. _mediaItem = [newMediaItem retain];
  26. }
  27. if (self.masterPopoverController != nil) {
  28. [self.masterPopoverController dismissPopoverAnimated:YES];
  29. }
  30. }
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. _mediaPlayer = [[VLCMediaPlayer alloc] init];
  35. [_mediaPlayer setDelegate:self];
  36. [_mediaPlayer setDrawable:self.movieView];
  37. self.navigationItem.leftBarButtonItem = self.backButton;
  38. self.navigationItem.titleView = self.positionSlider;
  39. self.navigationItem.rightBarButtonItem = self.timeDisplay;
  40. self.navigationItem.rightBarButtonItem.style = UIBarButtonItemStylePlain;
  41. }
  42. - (void)viewWillAppear:(BOOL)animated
  43. {
  44. [super viewWillAppear:animated];
  45. if (self.mediaItem) {
  46. self.title = [self.mediaItem title];
  47. [_mediaPlayer setMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:self.mediaItem.url]]];
  48. if (self.mediaItem.lastPosition && [self.mediaItem.lastPosition floatValue] < 0.99)
  49. [_mediaPlayer setPosition:[self.mediaItem.lastPosition floatValue]];
  50. [_mediaPlayer play];
  51. [UIApplication sharedApplication].idleTimerDisabled = YES;
  52. }
  53. self.tapBarView.hidden = NO;
  54. self.tapBarView.alpha = 1.0f;
  55. }
  56. - (void)viewWillDisappear:(BOOL)animated
  57. {
  58. [_mediaPlayer pause];
  59. [UIApplication sharedApplication].idleTimerDisabled = NO;
  60. [super viewWillDisappear:animated];
  61. }
  62. - (void)didReceiveMemoryWarning
  63. {
  64. [super didReceiveMemoryWarning];
  65. // Dispose of any resources that can be recreated.
  66. }
  67. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  68. {
  69. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  70. if (self)
  71. self.title = @"Video Playback";
  72. return self;
  73. }
  74. #pragma mark - controls
  75. - (IBAction)closePlayback:(id)sender
  76. {
  77. [self.navigationController popViewControllerAnimated:YES];
  78. }
  79. - (IBAction)positionSliderAction:(UISlider *)sender
  80. {
  81. _mediaPlayer.position = sender.value;
  82. }
  83. - (void)mediaPlayerTimeChanged:(NSNotification *)aNotification {
  84. self.positionSlider.value = [_mediaPlayer position];
  85. self.timeDisplay.title = [[_mediaPlayer remainingTime] stringValue];
  86. }
  87. - (IBAction)play:(id)sender
  88. {
  89. if ([_mediaPlayer isPlaying]) {
  90. [_mediaPlayer pause];
  91. _playPauseButton.titleLabel.text = @"Pse";
  92. } else {
  93. [_mediaPlayer play];
  94. _playPauseButton.titleLabel.text = @"Play";
  95. }
  96. }
  97. - (IBAction)forward:(id)sender
  98. {
  99. [_mediaPlayer mediumJumpForward];
  100. }
  101. - (IBAction)backward:(id)sender
  102. {
  103. [_mediaPlayer mediumJumpBackward];
  104. }
  105. - (IBAction)switchAudioTrack:(id)sender
  106. {
  107. _audiotrackActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Audio Track", @"audio track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  108. NSArray * audioTracks = [_mediaPlayer audioTracks];
  109. NSUInteger count = [audioTracks count];
  110. for (NSUInteger i = 1; i < count; i++) // skip the "Disable menu item"
  111. [_audiotrackActionSheet addButtonWithTitle:[audioTracks objectAtIndex:i]];
  112. [_audiotrackActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"audio track selector")];
  113. [_audiotrackActionSheet setCancelButtonIndex:[_audiotrackActionSheet numberOfButtons] - 1];
  114. [_audiotrackActionSheet showFromRect:[self.audioSwitcherButton frame] inView:self.audioSwitcherButton animated:YES];
  115. }
  116. - (IBAction)switchSubtitleTrack:(id)sender
  117. {
  118. _subtitleActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Choose Subtitle Track", @"subtitle track selector") delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
  119. NSArray * spuTracks = [_mediaPlayer videoSubTitles];
  120. NSUInteger count = [spuTracks count];
  121. for (NSUInteger i = 0; i < count; i++)
  122. [_subtitleActionSheet addButtonWithTitle:[spuTracks objectAtIndex:i]];
  123. [_subtitleActionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"subtitle track selector")];
  124. [_subtitleActionSheet setCancelButtonIndex:[_subtitleActionSheet numberOfButtons] - 1];
  125. [_subtitleActionSheet showFromRect:[self.subtitleSwitcherButton frame] inView:self.subtitleSwitcherButton animated:YES];
  126. }
  127. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  128. if (buttonIndex == 0) { // "Cancel" button
  129. APLog(@"action sheet was canceled");
  130. return;
  131. }
  132. if (actionSheet == _subtitleActionSheet) {
  133. _mediaPlayer.currentVideoSubTitleIndex = buttonIndex;
  134. [_subtitleActionSheet release];
  135. } else {
  136. _mediaPlayer.currentAudioTrackIndex = buttonIndex;
  137. [_audiotrackActionSheet release];
  138. }
  139. }
  140. #pragma mark - Split view
  141. - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
  142. {
  143. barButtonItem.title = NSLocalizedString(@"Master", @"Master");
  144. [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
  145. self.masterPopoverController = popoverController;
  146. }
  147. - (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
  148. {
  149. // Called when the view is shown again in the split view, invalidating the button and popover controller.
  150. [self.navigationItem setLeftBarButtonItem:nil animated:YES];
  151. self.masterPopoverController = nil;
  152. }
  153. @end