160 lines
5.1 KiB
Objective-C
160 lines
5.1 KiB
Objective-C
//
|
|
// CHMessageConentAudioView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2022/4/22.
|
|
//
|
|
|
|
#import "CHMessageConentAudioView.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
#import <NIMSDK/NIMSDK.h>
|
|
///Tool
|
|
|
|
#import "MEWThemeColor.h"
|
|
#import "CHMessageAudioCenter.h"
|
|
@interface CHMessageConentAudioView ()<NIMMediaManagerDelegate>
|
|
///背景
|
|
@property (nonatomic,strong) UIView * backView;
|
|
///显示语音的时长
|
|
@property (nonatomic,strong) UILabel *timeLabel;
|
|
///显示语音的动画
|
|
@property (nonatomic,strong) UIImageView *audioImageView;
|
|
///当前的消息
|
|
@property (nonatomic,strong) NIMMessage *message;
|
|
///是否正在播放
|
|
@property (nonatomic,assign) BOOL isPlaying;
|
|
@end
|
|
|
|
@implementation CHMessageConentAudioView
|
|
|
|
- (void)dealloc {
|
|
[[NIMSDK sharedSDK].mediaManager removeDelegate:self];
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[[NIMSDK sharedSDK].mediaManager setNeedProximityMonitor:NO];
|
|
[[NIMSDK sharedSDK].mediaManager addDelegate:self];
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
[self addSubview:self.backView];
|
|
[self.backView addSubview:self.timeLabel];
|
|
[self.backView addSubview:self.audioImageView];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(MESSAGE_PADDING, MESSAGE_PADDING, MESSAGE_PADDING, MESSAGE_PADDING));
|
|
make.height.mas_equalTo(30);
|
|
make.width.mas_equalTo(50);
|
|
}];
|
|
|
|
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.mas_equalTo(self.backView).offset(10);
|
|
make.centerY.mas_equalTo(self.backView);
|
|
}];
|
|
|
|
[self.audioImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(20, 20));
|
|
make.right.mas_equalTo(self.backView).offset(-10);
|
|
make.centerY.mas_equalTo(self.backView);
|
|
}];
|
|
}
|
|
|
|
#pragma mark - NIMMediaManagerDelegate
|
|
- (void)playAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
|
|
if(filePath && !error) {
|
|
if ([CHMessageAudioCenter shareInstance].currentPlayingMessage == self.message) {
|
|
[self.audioImageView startAnimating];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)playAudio:(NSString *)filePath didCompletedWithError:(NSError *)error {
|
|
[self.audioImageView stopAnimating];
|
|
UIImage * thirdImage = [UIImage imageNamed:@"mew_message_content_audio_playing_third"];
|
|
self.audioImageView.image = thirdImage;
|
|
}
|
|
#pragma mark - Event Response
|
|
- (void)didTapBackRecognizer {
|
|
if ([self.message attachmentDownloadState] == NIMMessageAttachmentDownloadStateDownloaded) {
|
|
if ([[NIMSDK sharedSDK].mediaManager isPlaying]) {
|
|
[[NIMSDK sharedSDK].mediaManager stopPlay];
|
|
}
|
|
|
|
if (self.isPlaying) {
|
|
[[NIMSDK sharedSDK].mediaManager stopPlay];
|
|
self.isPlaying = NO;
|
|
} else {
|
|
self.isPlaying = YES;
|
|
[[NIMSDK sharedSDK].mediaManager switchAudioOutputDevice:NIMAudioOutputDeviceSpeaker];
|
|
[[CHMessageAudioCenter shareInstance] playAudioMessage:self.message];
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - Getters And Setters
|
|
- (UIView *)backView {
|
|
if (!_backView) {
|
|
_backView = [[UIView alloc] init];
|
|
_backView.backgroundColor = [UIColor clearColor];
|
|
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapBackRecognizer)];
|
|
[_backView addGestureRecognizer:tap];
|
|
}
|
|
return _backView;
|
|
}
|
|
|
|
- (UILabel *)timeLabel {
|
|
if (!_timeLabel) {
|
|
_timeLabel = [[UILabel alloc] init];
|
|
_timeLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
|
|
_timeLabel.textColor = [MEWThemeColor mewMainTextColor];
|
|
}
|
|
return _timeLabel;
|
|
}
|
|
|
|
- (UIImageView *)audioImageView {
|
|
if (!_audioImageView) {
|
|
_audioImageView = [[UIImageView alloc] init];
|
|
_audioImageView.userInteractionEnabled = YES;
|
|
UIImage * firstImage = [UIImage imageNamed:@"mew_message_content_audio_playing_first"];
|
|
UIImage * secondImage = [UIImage imageNamed:@"mew_message_content_audio_playing_second"];
|
|
UIImage * thirdImage = [UIImage imageNamed:@"mew_message_content_audio_playing_third"];
|
|
_audioImageView.animationImages = @[firstImage, secondImage, thirdImage];
|
|
_audioImageView.animationDuration = 1;
|
|
_audioImageView.animationRepeatCount = HUGE;
|
|
_audioImageView.image = thirdImage;
|
|
}
|
|
return _audioImageView;
|
|
}
|
|
|
|
+ (CGFloat)measureHeight:(NIMMessage *)message {
|
|
NSInteger audioContentHeight = 30;
|
|
return (audioContentHeight + CONTENT_PADDING_V_TOTAL+ MESSAGE_PADDING * 2);
|
|
}
|
|
|
|
- (void)render:(NIMMessage *)message {
|
|
self.message = message;
|
|
NIMAudioObject *audioContent = (NIMAudioObject*)[message messageObject];
|
|
NSAssert([audioContent isKindOfClass:[NIMAudioObject class]], @"message should be audio");
|
|
CGFloat value = 2*atan((audioContent.duration/1000.0-1)/10.0)/M_PI;
|
|
NSInteger audioContentMinWidth = (CONTENT_WIDTH_MAX - 180);
|
|
NSInteger audioContentMaxWidth = (CONTENT_WIDTH_MAX - 100);
|
|
CGFloat audioWidth = (audioContentMaxWidth - audioContentMinWidth)* value + audioContentMinWidth;
|
|
[self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(audioWidth);;
|
|
}];
|
|
self.timeLabel.text = [NSString stringWithFormat:@"%zd\"",(audioContent.duration+500)/1000];//四舍五入
|
|
}
|
|
|
|
|
|
|
|
@end
|