VLCMiniPlaybackView.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. @interface VLCMiniPlaybackView () <VLCPlaybackControllerDelegate>
  17. {
  18. UIImageView *_artworkView;
  19. UIView *_videoView;
  20. UIButton *_previousButton;
  21. UIButton *_playPauseButton;
  22. UIButton *_nextButton;
  23. UIButton *_expandButton;
  24. UILabel *_metaDataLabel;
  25. }
  26. @property (nonatomic, weak) VLCPlaybackController *playbackController;
  27. @end
  28. @implementation VLCMiniPlaybackView
  29. - (instancetype)initWithFrame:(CGRect)viewFrame
  30. {
  31. self = [super initWithFrame:viewFrame];
  32. if (!self)
  33. return self;
  34. CGRect previousRect;
  35. CGFloat buttonSize = 44.;
  36. _artworkView = [[UIImageView alloc] initWithFrame:CGRectMake(0., 0., 60., 60.)];
  37. _artworkView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  38. _artworkView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  39. _artworkView.opaque = YES;
  40. [self addSubview:_artworkView];
  41. /* build buttons from right to left */
  42. _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
  43. [_expandButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
  44. _expandButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  45. [_expandButton addTarget:self action:@selector(pushFullPlaybackView:) forControlEvents:UIControlEventTouchUpInside];
  46. _expandButton.frame = previousRect = CGRectMake(viewFrame.size.width - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  47. [self addSubview:_expandButton];
  48. _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  49. [_nextButton setImage:[UIImage imageNamed:@"forwardIcon"] forState:UIControlStateNormal];
  50. [_nextButton sizeToFit];
  51. _nextButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  52. [_nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
  53. _nextButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  54. [self addSubview:_nextButton];
  55. _playPauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  56. [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
  57. [_playPauseButton sizeToFit];
  58. _playPauseButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  59. [_playPauseButton addTarget:self action:@selector(playPauseAction:) forControlEvents:UIControlEventTouchUpInside];
  60. _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
  61. _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
  62. _playPauseButton.isAccessibilityElement = YES;
  63. _playPauseButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  64. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
  65. [_playPauseButton addGestureRecognizer:longPressRecognizer];
  66. [self addSubview:_playPauseButton];
  67. _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
  68. [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
  69. [_previousButton sizeToFit];
  70. _previousButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  71. [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
  72. _previousButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  73. [self addSubview:_previousButton];
  74. CGFloat artworkViewWidth = _artworkView.frame.size.width;
  75. _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(artworkViewWidth + 10., 0., previousRect.origin.x - artworkViewWidth - 10., viewFrame.size.height)];
  76. _metaDataLabel.font = [UIFont systemFontOfSize:12.];
  77. _metaDataLabel.textColor = [UIColor VLCLightTextColor];
  78. _metaDataLabel.numberOfLines = 0;
  79. _metaDataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  80. [self addSubview:_metaDataLabel];
  81. return self;
  82. }
  83. - (void)previousAction:(id)sender
  84. {
  85. [self.playbackController backward];
  86. }
  87. - (void)playPauseAction:(id)sender
  88. {
  89. [self.playbackController playPause];
  90. }
  91. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  92. {
  93. switch (recognizer.state) {
  94. case UIGestureRecognizerStateBegan:
  95. [_playPauseButton setImage:[UIImage imageNamed:@"stopIcon"] forState:UIControlStateNormal];
  96. break;
  97. case UIGestureRecognizerStateEnded:
  98. [self.playbackController stopPlayback];
  99. break;
  100. case UIGestureRecognizerStateCancelled:
  101. case UIGestureRecognizerStateFailed:
  102. [self updatePlayPauseButton];
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. - (void)nextAction:(id)sender
  109. {
  110. [self.playbackController forward];
  111. }
  112. - (void)pushFullPlaybackView:(id)sender
  113. {
  114. [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
  115. }
  116. - (void)updatePlayPauseButton
  117. {
  118. const BOOL isPlaying = self.playbackController.isPlaying;
  119. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  120. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  121. }
  122. - (void)setupForWork:(VLCPlaybackController *)playbackController
  123. {
  124. self.playbackController = playbackController;
  125. [self updatePlayPauseButton];
  126. playbackController.delegate = self;
  127. [playbackController recoverDisplayedMetadata];
  128. }
  129. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  130. isPlaying:(BOOL)isPlaying
  131. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  132. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  133. forPlaybackController:(VLCPlaybackController *)controller
  134. {
  135. [self updatePlayPauseButton];
  136. }
  137. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
  138. title:(NSString *)title
  139. artwork:(UIImage *)artwork
  140. artist:(NSString *)artist
  141. album:(NSString *)album
  142. audioOnly:(BOOL)audioOnly
  143. {
  144. if (audioOnly) {
  145. _artworkView.contentMode = UIViewContentModeScaleAspectFill;
  146. _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
  147. if (_videoView) {
  148. [_videoView removeFromSuperview];
  149. _videoView = nil;
  150. }
  151. } else {
  152. _artworkView.image = nil;
  153. if (_videoView) {
  154. [_videoView removeFromSuperview];
  155. _videoView = nil;
  156. }
  157. _videoView = [[UIView alloc] initWithFrame:_artworkView.frame];
  158. [_videoView setClipsToBounds:YES];
  159. [self addSubview:_videoView];
  160. controller.videoOutputView = _videoView;
  161. }
  162. NSString *metaDataString;
  163. if (artist)
  164. metaDataString = artist;
  165. if (album)
  166. metaDataString = [metaDataString stringByAppendingFormat:@" — %@", album];
  167. if (metaDataString)
  168. metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", title];
  169. else
  170. metaDataString = title;
  171. _metaDataLabel.text = metaDataString;
  172. }
  173. @end