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