VLCMiniPlaybackView.m 9.9 KB

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