VLCMiniPlaybackView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*****************************************************************************
  2. * VLCMiniPlaybackView.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Author: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCMiniPlaybackView.h"
  13. #import "VLCPlaybackController.h"
  14. #import "VLCAppDelegate.h"
  15. #import "VLCPlaylistViewController.h"
  16. #import "VLCPlayerDisplayController.h"
  17. #import "VLCKeychainCoordinator.h"
  18. @interface VLCMiniPlaybackView () <VLCPlaybackControllerDelegate, UIGestureRecognizerDelegate>
  19. {
  20. UIImageView *_artworkView;
  21. UIView *_videoView;
  22. UIButton *_previousButton;
  23. UIButton *_playPauseButton;
  24. UIButton *_nextButton;
  25. UIButton *_expandButton;
  26. UILabel *_metaDataLabel;
  27. UITapGestureRecognizer *_labelTapRecognizer;
  28. UITapGestureRecognizer *_artworkTapRecognizer;
  29. }
  30. @property (nonatomic, weak) VLCPlaybackController *playbackController;
  31. @end
  32. @implementation VLCMiniPlaybackView
  33. - (void)dealloc
  34. {
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (instancetype)initWithFrame:(CGRect)viewFrame
  38. {
  39. self = [super initWithFrame:viewFrame];
  40. if (!self)
  41. return self;
  42. CGRect previousRect;
  43. CGFloat buttonSize = 44.;
  44. _artworkView = [[UIImageView alloc] initWithFrame:CGRectMake(0., 0., 60., 60.)];
  45. _artworkView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  46. _artworkView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  47. _artworkView.opaque = YES;
  48. [self addSubview:_artworkView];
  49. /* build buttons from right to left */
  50. _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
  51. [_expandButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
  52. _expandButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  53. [_expandButton addTarget:self action:@selector(pushFullPlaybackView:) forControlEvents:UIControlEventTouchUpInside];
  54. _expandButton.frame = previousRect = CGRectMake(viewFrame.size.width - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  55. _expandButton.accessibilityLabel = NSLocalizedString(@"FULLSCREEN_PLAYBACK", nil);
  56. [self addSubview:_expandButton];
  57. _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  58. [_nextButton setImage:[UIImage imageNamed:@"forwardIcon"] forState:UIControlStateNormal];
  59. [_nextButton sizeToFit];
  60. _nextButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  61. [_nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
  62. _nextButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  63. _nextButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
  64. [self addSubview:_nextButton];
  65. _playPauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  66. [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
  67. [_playPauseButton sizeToFit];
  68. _playPauseButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  69. [_playPauseButton addTarget:self action:@selector(playPauseAction:) forControlEvents:UIControlEventTouchUpInside];
  70. _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
  71. _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
  72. _playPauseButton.isAccessibilityElement = YES;
  73. _playPauseButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  74. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
  75. [_playPauseButton addGestureRecognizer:longPressRecognizer];
  76. [self addSubview:_playPauseButton];
  77. _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
  78. [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
  79. [_previousButton sizeToFit];
  80. _previousButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  81. [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
  82. _previousButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  83. _previousButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
  84. [self addSubview:_previousButton];
  85. CGFloat artworkViewWidth = _artworkView.frame.size.width;
  86. _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(artworkViewWidth + 10., 0., previousRect.origin.x - artworkViewWidth - 10., viewFrame.size.height)];
  87. _metaDataLabel.font = [UIFont systemFontOfSize:12.];
  88. _metaDataLabel.textColor = [UIColor VLCLightTextColor];
  89. _metaDataLabel.numberOfLines = 0;
  90. _metaDataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  91. [self addSubview:_metaDataLabel];
  92. _labelTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  93. _labelTapRecognizer.delegate = self;
  94. _labelTapRecognizer.numberOfTouchesRequired = 1;
  95. [_metaDataLabel addGestureRecognizer:_labelTapRecognizer];
  96. _metaDataLabel.userInteractionEnabled = YES;
  97. _artworkTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  98. _artworkTapRecognizer.delegate = self;
  99. _artworkTapRecognizer.numberOfTouchesRequired = 1;
  100. [_artworkView addGestureRecognizer:_artworkTapRecognizer];
  101. _artworkView.userInteractionEnabled = YES;
  102. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  103. [center addObserver:self
  104. selector:@selector(appBecameActive:)
  105. name:UIApplicationDidBecomeActiveNotification
  106. object:nil];
  107. [center addObserver:self
  108. selector:@selector(appBecameActive:)
  109. name:VLCPasscodeValidated
  110. object:nil];
  111. return self;
  112. }
  113. - (void)appBecameActive:(NSNotification *)aNotification
  114. {
  115. VLCPlayerDisplayController *pdc = [(VLCAppDelegate *)[UIApplication sharedApplication].delegate playerDisplayController];
  116. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  117. VLCPlaybackController *vpc = self.playbackController;
  118. [vpc recoverDisplayedMetadata];
  119. }
  120. }
  121. - (void)tapRecognized
  122. {
  123. [self pushFullPlaybackView:nil];
  124. }
  125. - (void)previousAction:(id)sender
  126. {
  127. [self.playbackController backward];
  128. }
  129. - (void)playPauseAction:(id)sender
  130. {
  131. [self.playbackController playPause];
  132. }
  133. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  134. {
  135. switch (recognizer.state) {
  136. case UIGestureRecognizerStateBegan:
  137. [_playPauseButton setImage:[UIImage imageNamed:@"stopIcon"] forState:UIControlStateNormal];
  138. break;
  139. case UIGestureRecognizerStateEnded:
  140. [self.playbackController stopPlayback];
  141. break;
  142. case UIGestureRecognizerStateCancelled:
  143. case UIGestureRecognizerStateFailed:
  144. [self updatePlayPauseButton];
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. - (void)nextAction:(id)sender
  151. {
  152. [self.playbackController forward];
  153. }
  154. - (void)pushFullPlaybackView:(id)sender
  155. {
  156. [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
  157. }
  158. - (void)updatePlayPauseButton
  159. {
  160. const BOOL isPlaying = self.playbackController.isPlaying;
  161. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  162. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  163. }
  164. - (void)setupForWork:(VLCPlaybackController *)playbackController
  165. {
  166. self.playbackController = playbackController;
  167. [self updatePlayPauseButton];
  168. playbackController.delegate = self;
  169. [playbackController recoverDisplayedMetadata];
  170. }
  171. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  172. isPlaying:(BOOL)isPlaying
  173. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  174. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  175. forPlaybackController:(VLCPlaybackController *)controller
  176. {
  177. [self updatePlayPauseButton];
  178. }
  179. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
  180. title:(NSString *)title
  181. artwork:(UIImage *)artwork
  182. artist:(NSString *)artist
  183. album:(NSString *)album
  184. audioOnly:(BOOL)audioOnly
  185. {
  186. if (audioOnly) {
  187. _artworkView.contentMode = UIViewContentModeScaleAspectFill;
  188. _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
  189. if (_videoView) {
  190. [_videoView removeFromSuperview];
  191. _videoView = nil;
  192. }
  193. [_artworkView addGestureRecognizer:_artworkTapRecognizer];
  194. } else {
  195. _artworkView.image = nil;
  196. if (_videoView) {
  197. [_videoView removeFromSuperview];
  198. _videoView = nil;
  199. }
  200. VLCPlayerDisplayController *pdc = [(VLCAppDelegate *)[UIApplication sharedApplication].delegate playerDisplayController];
  201. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  202. _videoView = [[UIView alloc] initWithFrame:_artworkView.frame];
  203. [_videoView setClipsToBounds:YES];
  204. [_videoView addGestureRecognizer:_artworkTapRecognizer];
  205. _videoView.userInteractionEnabled = YES;
  206. [self addSubview:_videoView];
  207. controller.videoOutputView = _videoView;
  208. }
  209. }
  210. NSString *metaDataString;
  211. if (artist)
  212. metaDataString = artist;
  213. if (album)
  214. metaDataString = [metaDataString stringByAppendingFormat:@" — %@", album];
  215. if (metaDataString)
  216. metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", title];
  217. else
  218. metaDataString = title;
  219. _metaDataLabel.text = metaDataString;
  220. }
  221. @end