VLCMiniPlaybackView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "VLCPlayerDisplayController.h"
  15. #import "VLCMetadata.h"
  16. #if TARGET_OS_IOS
  17. #import "VLCKeychainCoordinator.h"
  18. #endif
  19. @interface VLCMiniPlaybackView () <UIGestureRecognizerDelegate>
  20. {
  21. UIImageView *_artworkView;
  22. UIView *_videoView;
  23. UIButton *_previousButton;
  24. UIButton *_playPauseButton;
  25. UIButton *_nextButton;
  26. UIButton *_expandButton;
  27. UILabel *_metaDataLabel;
  28. UITapGestureRecognizer *_tapRecognizer;
  29. UIStackView *_stackView;
  30. }
  31. @end
  32. @implementation VLCMiniPlaybackView
  33. - (instancetype)initWithFrame:(CGRect)viewFrame
  34. {
  35. self = [super initWithFrame:viewFrame];
  36. if (self) {
  37. [self setupSubviews];
  38. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  39. [center addObserver:self
  40. selector:@selector(appBecameActive:)
  41. name:UIApplicationDidBecomeActiveNotification
  42. object:nil];
  43. #if TARGET_OS_IOS
  44. [center addObserver:self
  45. selector:@selector(appBecameActive:)
  46. name:VLCPasscodeValidated
  47. object:nil];
  48. #endif
  49. }
  50. return self;
  51. }
  52. - (void)setupSubviews
  53. {
  54. CGFloat buttonSize = 44.;
  55. CGFloat videoSize = 60.;
  56. CGFloat padding = 10.;
  57. _artworkView = [[UIImageView alloc] initWithFrame:CGRectZero];
  58. _artworkView.translatesAutoresizingMaskIntoConstraints = NO;
  59. _artworkView.clipsToBounds = YES;
  60. _artworkView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  61. _artworkView.opaque = YES;
  62. [self addSubview:_artworkView];
  63. _videoView = [[UIView alloc] initWithFrame:CGRectZero];
  64. [_videoView setClipsToBounds:YES];
  65. _videoView.userInteractionEnabled = NO;
  66. _videoView.translatesAutoresizingMaskIntoConstraints = NO;
  67. [self addSubview:_videoView];
  68. _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
  69. [_expandButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
  70. _expandButton.translatesAutoresizingMaskIntoConstraints = NO;
  71. [_expandButton addTarget:self action:@selector(pushFullPlaybackView:) forControlEvents:UIControlEventTouchUpInside];
  72. _expandButton.accessibilityLabel = NSLocalizedString(@"FULLSCREEN_PLAYBACK", nil);
  73. _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  74. [_nextButton setImage:[UIImage imageNamed:@"forwardIcon"] forState:UIControlStateNormal];
  75. _nextButton.translatesAutoresizingMaskIntoConstraints = NO;
  76. [_nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
  77. _nextButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
  78. _playPauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  79. [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
  80. _playPauseButton.translatesAutoresizingMaskIntoConstraints = NO;
  81. [_playPauseButton addTarget:self action:@selector(playPauseAction:) forControlEvents:UIControlEventTouchUpInside];
  82. _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
  83. _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
  84. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
  85. [_playPauseButton addGestureRecognizer:longPressRecognizer];
  86. _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
  87. [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
  88. [_previousButton sizeToFit];
  89. _previousButton.translatesAutoresizingMaskIntoConstraints = NO;
  90. [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
  91. _previousButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
  92. _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  93. _metaDataLabel.font = [UIFont systemFontOfSize:12.];
  94. _metaDataLabel.textColor = [UIColor VLCLightTextColor];
  95. _metaDataLabel.numberOfLines = 0;
  96. _metaDataLabel.translatesAutoresizingMaskIntoConstraints = NO;
  97. [self addSubview:_metaDataLabel];
  98. _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[_previousButton, _playPauseButton, _nextButton, _expandButton]];
  99. _stackView.translatesAutoresizingMaskIntoConstraints = NO;
  100. _stackView.distribution = UIStackViewDistributionFillEqually;
  101. [self addSubview:_stackView];
  102. UILayoutGuide *guide = self;
  103. if (@available(iOS 11.0, *)) {
  104. guide = self.safeAreaLayoutGuide;
  105. }
  106. [NSLayoutConstraint activateConstraints:@[
  107. [_artworkView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
  108. [_artworkView.topAnchor constraintEqualToAnchor:self.topAnchor],
  109. [_artworkView.rightAnchor constraintEqualToAnchor:_metaDataLabel.leftAnchor constant:-padding],
  110. [_artworkView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  111. [_artworkView.widthAnchor constraintEqualToConstant:videoSize],
  112. [_artworkView.heightAnchor constraintEqualToAnchor:_artworkView.widthAnchor],
  113. [_videoView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
  114. [_videoView.topAnchor constraintEqualToAnchor:self.topAnchor],
  115. [_videoView.rightAnchor constraintEqualToAnchor:_metaDataLabel.leftAnchor constant:-padding],
  116. [_videoView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  117. [_videoView.widthAnchor constraintEqualToConstant:videoSize],
  118. [_videoView.heightAnchor constraintEqualToAnchor:_videoView.widthAnchor],
  119. [_metaDataLabel.topAnchor constraintEqualToAnchor:self.topAnchor],
  120. [_metaDataLabel.rightAnchor constraintLessThanOrEqualToAnchor:_stackView.leftAnchor constant:- padding],
  121. [_metaDataLabel.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  122. [_previousButton.widthAnchor constraintEqualToConstant:buttonSize],
  123. [_stackView.topAnchor constraintEqualToAnchor:self.topAnchor],
  124. [_stackView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
  125. [_stackView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  126. ]];
  127. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  128. _tapRecognizer.delegate = self;
  129. [self addGestureRecognizer:_tapRecognizer];
  130. #if TARGET_OS_IOS
  131. _tapRecognizer.numberOfTouchesRequired = 1;
  132. #endif
  133. }
  134. - (void)appBecameActive:(NSNotification *)aNotification
  135. {
  136. VLCPlayerDisplayController *pdc = [VLCPlayerDisplayController sharedInstance];
  137. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  138. [[VLCPlaybackController sharedInstance] recoverDisplayedMetadata];
  139. }
  140. }
  141. - (void)tapRecognized
  142. {
  143. [self pushFullPlaybackView:nil];
  144. }
  145. - (void)previousAction:(id)sender
  146. {
  147. [[VLCPlaybackController sharedInstance] previous];
  148. }
  149. - (void)playPauseAction:(id)sender
  150. {
  151. [[VLCPlaybackController sharedInstance] playPause];
  152. }
  153. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  154. {
  155. switch (recognizer.state) {
  156. case UIGestureRecognizerStateBegan:
  157. [_playPauseButton setImage:[UIImage imageNamed:@"stopIcon"] forState:UIControlStateNormal];
  158. break;
  159. case UIGestureRecognizerStateEnded:
  160. [[VLCPlaybackController sharedInstance] stopPlayback];
  161. break;
  162. case UIGestureRecognizerStateCancelled:
  163. case UIGestureRecognizerStateFailed:
  164. [self updatePlayPauseButton];
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. - (void)nextAction:(id)sender
  171. {
  172. [[VLCPlaybackController sharedInstance] next];
  173. }
  174. - (void)pushFullPlaybackView:(id)sender
  175. {
  176. [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
  177. }
  178. - (void)updatePlayPauseButton
  179. {
  180. const BOOL isPlaying = [VLCPlaybackController sharedInstance].isPlaying;
  181. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  182. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  183. }
  184. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  185. {
  186. [self updatePlayPauseButton];
  187. controller.delegate = self;
  188. [controller recoverDisplayedMetadata];
  189. }
  190. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  191. isPlaying:(BOOL)isPlaying
  192. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  193. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  194. forPlaybackController:(VLCPlaybackController *)controller
  195. {
  196. [self updatePlayPauseButton];
  197. }
  198. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller metadata:(VLCMetaData *)metadata
  199. {
  200. _videoView.hidden = YES;
  201. if (metadata.isAudioOnly) {
  202. _artworkView.contentMode = UIViewContentModeScaleAspectFill;
  203. _artworkView.image = metadata.artworkImage?: [UIImage imageNamed:@"no-artwork"];
  204. } else {
  205. _artworkView.image = nil;
  206. VLCPlayerDisplayController *pdc = [VLCPlayerDisplayController sharedInstance];
  207. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  208. _videoView.hidden = false;
  209. controller.videoOutputView = _videoView;
  210. }
  211. }
  212. NSString *metaDataString;
  213. if (metadata.artist)
  214. metaDataString = metadata.artist;
  215. if (metadata.albumName)
  216. metaDataString = [metaDataString stringByAppendingFormat:@" — %@", metadata.albumName];
  217. if (metaDataString)
  218. metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", metadata.title];
  219. else
  220. metaDataString = metadata.title;
  221. _metaDataLabel.text = metaDataString;
  222. }
  223. @end