VLCMiniPlaybackView.m 10 KB

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