VLCMiniPlaybackView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #if TARGET_OS_IOS
  16. #import "VLCLibraryViewController.h"
  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 *_labelTapRecognizer;
  29. UITapGestureRecognizer *_artworkTapRecognizer;
  30. }
  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.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. [_metaDataLabel addGestureRecognizer:_labelTapRecognizer];
  94. _metaDataLabel.userInteractionEnabled = YES;
  95. _artworkTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  96. _artworkTapRecognizer.delegate = self;
  97. [_artworkView addGestureRecognizer:_artworkTapRecognizer];
  98. _artworkView.userInteractionEnabled = YES;
  99. #if TARGET_OS_IOS
  100. _labelTapRecognizer.numberOfTouchesRequired = 1;
  101. _artworkTapRecognizer.numberOfTouchesRequired = 1;
  102. #endif
  103. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  104. [center addObserver:self
  105. selector:@selector(appBecameActive:)
  106. name:UIApplicationDidBecomeActiveNotification
  107. object:nil];
  108. #if TARGET_OS_IOS
  109. [center addObserver:self
  110. selector:@selector(appBecameActive:)
  111. name:VLCPasscodeValidated
  112. object:nil];
  113. #endif
  114. return self;
  115. }
  116. - (void)appBecameActive:(NSNotification *)aNotification
  117. {
  118. VLCPlayerDisplayController *pdc = [VLCPlayerDisplayController sharedInstance];
  119. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  120. [[VLCPlaybackController sharedInstance] recoverDisplayedMetadata];
  121. }
  122. }
  123. - (void)tapRecognized
  124. {
  125. [self pushFullPlaybackView:nil];
  126. }
  127. - (void)previousAction:(id)sender
  128. {
  129. [[VLCPlaybackController sharedInstance] backward];
  130. }
  131. - (void)playPauseAction:(id)sender
  132. {
  133. [[VLCPlaybackController sharedInstance] playPause];
  134. }
  135. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  136. {
  137. switch (recognizer.state) {
  138. case UIGestureRecognizerStateBegan:
  139. [_playPauseButton setImage:[UIImage imageNamed:@"stopIcon"] forState:UIControlStateNormal];
  140. break;
  141. case UIGestureRecognizerStateEnded:
  142. [[VLCPlaybackController sharedInstance] stopPlayback];
  143. break;
  144. case UIGestureRecognizerStateCancelled:
  145. case UIGestureRecognizerStateFailed:
  146. [self updatePlayPauseButton];
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. - (void)nextAction:(id)sender
  153. {
  154. [[VLCPlaybackController sharedInstance] forward];
  155. }
  156. - (void)pushFullPlaybackView:(id)sender
  157. {
  158. [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
  159. }
  160. - (void)updatePlayPauseButton
  161. {
  162. const BOOL isPlaying = [VLCPlaybackController sharedInstance].isPlaying;
  163. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  164. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  165. }
  166. - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
  167. {
  168. [self updatePlayPauseButton];
  169. controller.delegate = self;
  170. [controller recoverDisplayedMetadata];
  171. }
  172. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  173. isPlaying:(BOOL)isPlaying
  174. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  175. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  176. forPlaybackController:(VLCPlaybackController *)controller
  177. {
  178. [self updatePlayPauseButton];
  179. }
  180. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
  181. title:(NSString *)title
  182. artwork:(UIImage *)artwork
  183. artist:(NSString *)artist
  184. album:(NSString *)album
  185. audioOnly:(BOOL)audioOnly
  186. {
  187. if (audioOnly) {
  188. _artworkView.contentMode = UIViewContentModeScaleAspectFill;
  189. _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
  190. if (_videoView) {
  191. [_videoView removeFromSuperview];
  192. _videoView = nil;
  193. }
  194. [_artworkView addGestureRecognizer:_artworkTapRecognizer];
  195. } else {
  196. _artworkView.image = nil;
  197. if (_videoView) {
  198. [_videoView removeFromSuperview];
  199. _videoView = nil;
  200. }
  201. VLCPlayerDisplayController *pdc = [VLCPlayerDisplayController sharedInstance];
  202. if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
  203. _videoView = [[UIView alloc] initWithFrame:_artworkView.frame];
  204. [_videoView setClipsToBounds:YES];
  205. [_videoView addGestureRecognizer:_artworkTapRecognizer];
  206. _videoView.userInteractionEnabled = YES;
  207. [self addSubview:_videoView];
  208. controller.videoOutputView = _videoView;
  209. }
  210. }
  211. NSString *metaDataString;
  212. if (artist)
  213. metaDataString = artist;
  214. if (album)
  215. metaDataString = [metaDataString stringByAppendingFormat:@" — %@", album];
  216. if (metaDataString)
  217. metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", title];
  218. else
  219. metaDataString = title;
  220. _metaDataLabel.text = metaDataString;
  221. }
  222. @end