VLCMiniPlaybackView.m 9.9 KB

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