YOUPAILZMusicPlayerView.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // YOUPAILZMusicPlayerView.m
  3. // VQU
  4. //
  5. // Created by CY on 2021/6/26.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZMusicPlayerView.h"
  9. #import "YOUPAILZMusicLyricCell.h"
  10. #import <AgoraRtcKit/AgoraRtcEngineKit.h>
  11. @interface YOUPAILZMusicPlayerView () <UITableViewDataSource,UITableViewDelegate>
  12. @property (nonatomic, weak) UIButton *youpaipcoverBtn;
  13. @property (nonatomic, weak) UITableView *youpaiptableView;
  14. @property (nonatomic, strong) YOUPAILZMusicModel *youpaipmusicModel;
  15. @property (nonatomic, assign) NSInteger youpaipcurrentRow;
  16. @property (nonatomic, strong) NSTimer *youpaiptimer;
  17. @end
  18. @implementation YOUPAILZMusicPlayerView
  19. - (instancetype)initWithFrame:(CGRect)frame{
  20. if (self = [super initWithFrame:frame]) {
  21. [self youpaifinitUI];
  22. [self youpaifstartTimer];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicPlayStateChangeNotification:) name:@"MusicPlayStateChangeNotification" object:nil];
  24. }
  25. return self;
  26. }
  27. ///// 播放状态发生改变
  28. //- (void)youpaifmusicPlayStateChangeNotification:(NSNotification *)notification{
  29. // AgoraAudioMixingStateCode stateCode = [notification.object integerValue];
  30. // if (stateCode == AgoraAudioMixingStatePlaying) {
  31. // [self youpaifresumeAnimation];
  32. // }else{
  33. // [self youpaifpauseAnimation];
  34. // }
  35. // self.youpaipcoverBtn.selected = stateCode != AgoraAudioMixingStatePlaying;
  36. //}
  37. - (void)youpaifinitUI{
  38. UIButton *youpaipcoverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  39. youpaipcoverBtn.layer.cornerRadius = 30.0f;
  40. youpaipcoverBtn.contentMode = UIViewContentModeScaleAspectFill;
  41. youpaipcoverBtn.clipsToBounds = YES;
  42. youpaipcoverBtn.backgroundColor = [HexColorFromRGB(0x2E2B40) colorWithAlphaComponent:0.94f];
  43. youpaipcoverBtn.layer.borderColor = [[UIColor whiteColor] colorWithAlphaComponent:0.4f].CGColor;
  44. youpaipcoverBtn.layer.borderWidth = 2.0f;
  45. [youpaipcoverBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_play"] forState:UIControlStateNormal];
  46. [youpaipcoverBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_suspend"] forState:UIControlStateSelected];
  47. [youpaipcoverBtn addTarget:self action:@selector(youpaifcoverBtnClick) forControlEvents:UIControlEventTouchUpInside];
  48. [self addSubview:youpaipcoverBtn];
  49. self.youpaipcoverBtn = youpaipcoverBtn;
  50. [youpaipcoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.right.offset(-8.0f);
  52. make.centerY.equalTo(self);
  53. make.size.mas_offset(CGSizeMake(60.0f, 60.0f));
  54. }];
  55. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  56. tableView.delegate = self;
  57. tableView.dataSource = self;
  58. tableView.userInteractionEnabled = NO;
  59. tableView.rowHeight = 34.0f;
  60. tableView.estimatedRowHeight = 34.0f;
  61. tableView.estimatedSectionHeaderHeight = 0.0f;
  62. tableView.estimatedSectionFooterHeight = 0.0f;
  63. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  64. tableView.backgroundColor = [UIColor clearColor];
  65. tableView.showsVerticalScrollIndicator = NO;
  66. tableView.clipsToBounds = YES;
  67. [self addSubview:tableView];
  68. self.youpaiptableView = tableView;
  69. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.left.bottom.offset(0.0f);
  71. make.top.offset(0.0f);
  72. make.right.equalTo(youpaipcoverBtn.mas_left).offset(-8.0f);
  73. }];
  74. }
  75. - (void)youpaifcoverBtnClick{
  76. if (self.youpaipsuspendMusicBlock != nil) {
  77. self.youpaipsuspendMusicBlock();
  78. }
  79. }
  80. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  81. return self.youpaipmusicModel.youpaipwordArray.count;
  82. }
  83. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  84. NSString *cellID = @"YOUPAILZMusicLyricCell";
  85. YOUPAILZMusicLyricCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  86. if (cell == nil) {
  87. cell = [[YOUPAILZMusicLyricCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  88. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  89. }
  90. NSString *text = self.youpaipmusicModel.youpaipwordArray[indexPath.row];
  91. text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
  92. text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""];
  93. text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  94. [self youpaifsetupShadowWithText:text view:cell.youpaiptextL];
  95. if(indexPath.row == self.youpaipcurrentRow){
  96. cell.youpaiptextL.textColor = [UIColor whiteColor];
  97. cell.youpaiptextL.font = LCBoldFont(14.0f);
  98. }else{
  99. cell.youpaiptextL.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8f];
  100. cell.youpaiptextL.font = LCFont14;
  101. }
  102. cell.youpaiptextL.textAlignment = NSTextAlignmentRight;
  103. cell.youpaiptextL.numberOfLines = 2;
  104. return cell;
  105. }
  106. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  107. return CGFLOAT_MIN;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  110. return CGFLOAT_MIN;
  111. }
  112. - (void)youpaifupdateTime{
  113. CGFloat currentTime = 0.0f;
  114. if (self.getPlayPositionBlock != nil) {
  115. currentTime = self.getPlayPositionBlock() * 0.001f;
  116. }
  117. NSLog(@"%d:%d",(int)currentTime / 60, (int)currentTime % 60);
  118. for (int i = 0; i < self.youpaipmusicModel.youpaiptimerArray.count; i ++) {
  119. NSArray *timeArray=[self.youpaipmusicModel.youpaiptimerArray[i] componentsSeparatedByString:@":"];
  120. float youpaiplrcTime = [timeArray[0] intValue] * 60 + [timeArray[1] floatValue];
  121. if(currentTime>youpaiplrcTime){
  122. self.youpaipcurrentRow = i;
  123. }else
  124. break;
  125. }
  126. [self.youpaiptableView reloadData];
  127. if (self.youpaipmusicModel.youpaipwordArray.count != 0 && self.youpaipcurrentRow < self.youpaipmusicModel.youpaipwordArray.count) {
  128. [self.youpaiptableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.youpaipcurrentRow inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  129. }
  130. }
  131. - (void)youpaifreloadMusicModel:(YOUPAILZMusicModel *)youpaipmusicModel{
  132. self.youpaipmusicModel = youpaipmusicModel;
  133. [self.youpaipcoverBtn sd_setImageWithURL:[NSURL URLWithString:youpaipmusicModel.youpaipposter] forState:UIControlStateNormal];
  134. [self.youpaiptableView reloadData];
  135. }
  136. - (void)youpaifstartTimer{
  137. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(youpaifupdateTime) userInfo:nil repeats:YES];
  138. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  139. }
  140. //暂停动画
  141. - (void)youpaifpauseAnimation {
  142. [self.youpaipcoverBtn.layer removeAnimationForKey:@"rotationAnimation"];
  143. }
  144. //恢复动画
  145. - (void)youpaifresumeAnimation {
  146. CABasicAnimation *rotationAnimation;
  147. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  148. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
  149. rotationAnimation.duration = 10.0f;
  150. rotationAnimation.cumulative = YES;
  151. rotationAnimation.repeatCount = CGFLOAT_MAX;
  152. rotationAnimation.removedOnCompletion = NO;
  153. [self.youpaipcoverBtn.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  154. }
  155. - (void)youpaifsetupShadowWithText:(NSString *)text view:(UILabel *)view{
  156. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
  157. NSShadow *shadow = [[NSShadow alloc]init];
  158. shadow.shadowOffset = CGSizeMake(1, 1.5f);
  159. shadow.shadowColor = [HexColorFromRGB(0x000000) colorWithAlphaComponent:0.2f];
  160. [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
  161. view.attributedText = attributedString;
  162. }
  163. - (void)setShowLyric:(BOOL)showLyric{
  164. _showLyric = showLyric;
  165. self.youpaiptableView.hidden = !showLyric;
  166. }
  167. - (void)dealloc{
  168. [self youpaifstopTimer];
  169. [[NSNotificationCenter defaultCenter] removeObserver:self];
  170. }
  171. - (void)youpaifstopTimer{
  172. [self.youpaiptimer invalidate];
  173. self.youpaiptimer = nil;
  174. }
  175. @end