// // AudioPlayerView.m // Meet // // Created by mambaxie on 2021/1/3. // #import "AudioPlayerView.h" #import "DDAudioManager.h" static AudioPlayerView *gCurrentPlayingPlayerView; @interface AudioPlayerView() @end @implementation AudioPlayerView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { if (CGSizeEqualToSize(self.size, CGSizeZero)) { self.size = CGSizeMake(FIX_SIZE(180), FIX_SIZE(34)); } self.layer.cornerRadius = 5.0; self.backgroundColor = THEME_COLOR; UIImageView *voiceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25 * 1.2, 15 * 1.2)]; voiceImageView.contentMode = UIViewContentModeScaleAspectFit; UIColor *iconColor = COLOR_WITH_RGB(0x333333); voiceImageView.image = [ImageNamed(@"message_voice_sender_normal") mt_imageWithTintColor:iconColor]; voiceImageView.animationImages = @[[ImageNamed(@"message_voice_sender_playing_1") mt_imageWithTintColor:iconColor], [ImageNamed(@"message_voice_sender_playing_2") mt_imageWithTintColor:iconColor],[ImageNamed(@"message_voice_sender_playing_3") mt_imageWithTintColor:iconColor]]; voiceImageView.animationDuration = 2.0; voiceImageView.animationRepeatCount = 0; voiceImageView.userInteractionEnabled = NO; voiceImageView.backgroundColor = UIColor.clearColor; voiceImageView.centerY = self.height * 0.5; voiceImageView.right = self.width - FIX_SIZE(10); self.voiceImageView = voiceImageView; [self addSubview:voiceImageView]; UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectZero]; secondLabel.x = FIX_SIZE(10); secondLabel.width = voiceImageView.x - 5 - secondLabel.x; secondLabel.height = self.height; secondLabel.textAlignment = NSTextAlignmentLeft; [secondLabel setFont:FONT_SIZE(12)]; [secondLabel setTextColor:iconColor]; self.secondLabel = secondLabel; [self addSubview:secondLabel]; WeakSelf(self); [self addTapWithAction:^{ if (weakself.checkCanPlay) { if (!weakself.checkCanPlay(weakself)) { // 不允许播放 return; } } if ([gCurrentPlayingPlayerView.voiceImageView isAnimating]) { // 正在播放 [gCurrentPlayingPlayerView.voiceImageView stopAnimating]; [[DDAudioManager shareInstance] stopPlay]; if (gCurrentPlayingPlayerView == weakself) { // 自己 return; } } [weakself.voiceImageView startAnimating]; gCurrentPlayingPlayerView = weakself; // 下载完成再播放 [[DDAudioManager shareInstance] playAudioWithUrl:weakself.audioUrl Method:DiskThenHttp didFinish:^(NSError * error){ [gCurrentPlayingPlayerView.voiceImageView stopAnimating]; if (error) { [SVProgressHUD showErrorWithStatus:[error localizedDescription]]; } }]; }]; } - (void)layoutSubviews { [super layoutSubviews]; self.secondLabel.centerY = self.voiceImageView.centerY = self.height * 0.5; } - (void)dealloc { if (self == gCurrentPlayingPlayerView) { [self.class stopPlay]; } } - (void)willMoveToWindow:(UIWindow *)newWindow { if (!newWindow) { // y不在屏幕上 if (self == gCurrentPlayingPlayerView) { [self.class stopPlay]; } } } - (void)updateWithAudioUrl:(NSString *)audioUrl seconds:(NSInteger)seconds { self.audioUrl = audioUrl; self.secondLabel.text = [NSString stringWithFormat:@"%ld\"", (long)seconds]; self.seconds = seconds; } + (void)stopPlay { [gCurrentPlayingPlayerView.voiceImageView stopAnimating]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ [[DDAudioManager shareInstance] stopPlay]; }); gCurrentPlayingPlayerView = nil; } + (AudioPlayerView *)playingView { return gCurrentPlayingPlayerView; } @end