VLCMiniPlaybackView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  61. [self addSubview:_playPauseButton];
  62. _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
  63. [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
  64. [_previousButton sizeToFit];
  65. _previousButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  66. [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
  67. _previousButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
  68. [self addSubview:_previousButton];
  69. CGFloat artworkViewWidth = _artworkView.frame.size.width;
  70. _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(artworkViewWidth + 10., 0., previousRect.origin.x - artworkViewWidth - 10., viewFrame.size.height)];
  71. _metaDataLabel.font = [UIFont systemFontOfSize:12.];
  72. _metaDataLabel.textColor = [UIColor VLCLightTextColor];
  73. _metaDataLabel.numberOfLines = 0;
  74. _metaDataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  75. [self addSubview:_metaDataLabel];
  76. return self;
  77. }
  78. - (void)previousAction:(id)sender
  79. {
  80. [self.playbackController backward];
  81. }
  82. - (void)playPauseAction:(id)sender
  83. {
  84. [self.playbackController playPause];
  85. }
  86. - (void)nextAction:(id)sender
  87. {
  88. [self.playbackController forward];
  89. }
  90. - (void)pushFullPlaybackView:(id)sender
  91. {
  92. [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
  93. }
  94. - (void)setupForWork:(VLCPlaybackController *)playbackController
  95. {
  96. self.playbackController = playbackController;
  97. if (playbackController.isPlaying)
  98. [_playPauseButton setImage:[UIImage imageNamed:@"pauseIcon"] forState:UIControlStateNormal];
  99. else
  100. [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
  101. playbackController.delegate = self;
  102. [playbackController recoverDisplayedMetadata];
  103. }
  104. - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
  105. isPlaying:(BOOL)isPlaying
  106. currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
  107. currentMediaHasChapters:(BOOL)currentMediaHasChapters
  108. forPlaybackController:(VLCPlaybackController *)controller
  109. {
  110. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  111. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  112. }
  113. - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
  114. title:(NSString *)title
  115. artwork:(UIImage *)artwork
  116. artist:(NSString *)artist
  117. album:(NSString *)album
  118. audioOnly:(BOOL)audioOnly
  119. {
  120. if (audioOnly) {
  121. _artworkView.contentMode = UIViewContentModeScaleAspectFill;
  122. _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
  123. if (_videoView) {
  124. [_videoView removeFromSuperview];
  125. _videoView = nil;
  126. }
  127. } else {
  128. _artworkView.image = nil;
  129. if (_videoView) {
  130. [_videoView removeFromSuperview];
  131. _videoView = nil;
  132. }
  133. _videoView = [[UIView alloc] initWithFrame:_artworkView.frame];
  134. [_videoView setClipsToBounds:YES];
  135. [self addSubview:_videoView];
  136. controller.videoOutputView = _videoView;
  137. }
  138. NSString *metaDataString;
  139. if (artist)
  140. metaDataString = artist;
  141. if (album)
  142. metaDataString = [metaDataString stringByAppendingFormat:@" — %@", album];
  143. if (metaDataString)
  144. metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", title];
  145. else
  146. metaDataString = title;
  147. _metaDataLabel.text = metaDataString;
  148. }
  149. @end