// // YOUPAILZMusicPlayerView.m // VQU // // Created by CY on 2021/6/26. // Copyright © 2021 leo. All rights reserved. // #import "YOUPAILZMusicPlayerView.h" #import "YOUPAILZMusicLyricCell.h" #import @interface YOUPAILZMusicPlayerView () @property (nonatomic, weak) UIButton *youpaipcoverBtn; @property (nonatomic, weak) UITableView *youpaiptableView; @property (nonatomic, strong) YOUPAILZMusicModel *youpaipmusicModel; @property (nonatomic, assign) NSInteger youpaipcurrentRow; @property (nonatomic, strong) NSTimer *youpaiptimer; @end @implementation YOUPAILZMusicPlayerView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self youpaifinitUI]; [self youpaifstartTimer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicPlayStateChangeNotification:) name:@"MusicPlayStateChangeNotification" object:nil]; } return self; } ///// 播放状态发生改变 //- (void)youpaifmusicPlayStateChangeNotification:(NSNotification *)notification{ // AgoraAudioMixingStateCode stateCode = [notification.object integerValue]; // if (stateCode == AgoraAudioMixingStatePlaying) { // [self youpaifresumeAnimation]; // }else{ // [self youpaifpauseAnimation]; // } // self.youpaipcoverBtn.selected = stateCode != AgoraAudioMixingStatePlaying; //} - (void)youpaifinitUI{ UIButton *youpaipcoverBtn = [UIButton buttonWithType:UIButtonTypeCustom]; youpaipcoverBtn.layer.cornerRadius = 30.0f; youpaipcoverBtn.contentMode = UIViewContentModeScaleAspectFill; youpaipcoverBtn.clipsToBounds = YES; youpaipcoverBtn.backgroundColor = [HexColorFromRGB(0x2E2B40) colorWithAlphaComponent:0.94f]; youpaipcoverBtn.layer.borderColor = [[UIColor whiteColor] colorWithAlphaComponent:0.4f].CGColor; youpaipcoverBtn.layer.borderWidth = 2.0f; [youpaipcoverBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_play"] forState:UIControlStateNormal]; [youpaipcoverBtn setImage:[UIImage imageNamed:@"vqu_images_L_live_music_player_suspend"] forState:UIControlStateSelected]; [youpaipcoverBtn addTarget:self action:@selector(youpaifcoverBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:youpaipcoverBtn]; self.youpaipcoverBtn = youpaipcoverBtn; [youpaipcoverBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.offset(-8.0f); make.centerY.equalTo(self); make.size.mas_offset(CGSizeMake(60.0f, 60.0f)); }]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.userInteractionEnabled = NO; tableView.rowHeight = 34.0f; tableView.estimatedRowHeight = 34.0f; tableView.estimatedSectionHeaderHeight = 0.0f; tableView.estimatedSectionFooterHeight = 0.0f; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor clearColor]; tableView.showsVerticalScrollIndicator = NO; tableView.clipsToBounds = YES; [self addSubview:tableView]; self.youpaiptableView = tableView; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.offset(0.0f); make.top.offset(0.0f); make.right.equalTo(youpaipcoverBtn.mas_left).offset(-8.0f); }]; } - (void)youpaifcoverBtnClick{ if (self.youpaipsuspendMusicBlock != nil) { self.youpaipsuspendMusicBlock(); } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.youpaipmusicModel.youpaipwordArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *cellID = @"YOUPAILZMusicLyricCell"; YOUPAILZMusicLyricCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { cell = [[YOUPAILZMusicLyricCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSString *text = self.youpaipmusicModel.youpaipwordArray[indexPath.row]; text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符 text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""]; text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""]; [self youpaifsetupShadowWithText:text view:cell.youpaiptextL]; if(indexPath.row == self.youpaipcurrentRow){ cell.youpaiptextL.textColor = [UIColor whiteColor]; cell.youpaiptextL.font = LCBoldFont(14.0f); }else{ cell.youpaiptextL.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8f]; cell.youpaiptextL.font = LCFont14; } cell.youpaiptextL.textAlignment = NSTextAlignmentRight; cell.youpaiptextL.numberOfLines = 2; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return CGFLOAT_MIN; } - (void)youpaifupdateTime{ CGFloat currentTime = 0.0f; if (self.getPlayPositionBlock != nil) { currentTime = self.getPlayPositionBlock() * 0.001f; } NSLog(@"%d:%d",(int)currentTime / 60, (int)currentTime % 60); for (int i = 0; i < self.youpaipmusicModel.youpaiptimerArray.count; i ++) { NSArray *timeArray=[self.youpaipmusicModel.youpaiptimerArray[i] componentsSeparatedByString:@":"]; float youpaiplrcTime = [timeArray[0] intValue] * 60 + [timeArray[1] floatValue]; if(currentTime>youpaiplrcTime){ self.youpaipcurrentRow = i; }else break; } [self.youpaiptableView reloadData]; if (self.youpaipmusicModel.youpaipwordArray.count != 0 && self.youpaipcurrentRow < self.youpaipmusicModel.youpaipwordArray.count) { [self.youpaiptableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.youpaipcurrentRow inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } } - (void)youpaifreloadMusicModel:(YOUPAILZMusicModel *)youpaipmusicModel{ self.youpaipmusicModel = youpaipmusicModel; [self.youpaipcoverBtn sd_setImageWithURL:[NSURL URLWithString:youpaipmusicModel.youpaipposter] forState:UIControlStateNormal]; [self.youpaiptableView reloadData]; } - (void)youpaifstartTimer{ self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(youpaifupdateTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes]; } //暂停动画 - (void)youpaifpauseAnimation { [self.youpaipcoverBtn.layer removeAnimationForKey:@"rotationAnimation"]; } //恢复动画 - (void)youpaifresumeAnimation { CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ]; rotationAnimation.duration = 10.0f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = CGFLOAT_MAX; rotationAnimation.removedOnCompletion = NO; [self.youpaipcoverBtn.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } - (void)youpaifsetupShadowWithText:(NSString *)text view:(UILabel *)view{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text]; NSShadow *shadow = [[NSShadow alloc]init]; shadow.shadowOffset = CGSizeMake(1, 1.5f); shadow.shadowColor = [HexColorFromRGB(0x000000) colorWithAlphaComponent:0.2f]; [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)]; view.attributedText = attributedString; } - (void)setShowLyric:(BOOL)showLyric{ _showLyric = showLyric; self.youpaiptableView.hidden = !showLyric; } - (void)dealloc{ [self youpaifstopTimer]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)youpaifstopTimer{ [self.youpaiptimer invalidate]; self.youpaiptimer = nil; } @end