123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //
- // SessionAudioCententView.m
- // NIMDemo
- //
- // Created by chris.
- // Copyright (c) 2015年 Netease. All rights reserved.
- //
- #import "NIMSessionAudioContentView.h"
- #import "UIView+NIM.h"
- #import "NIMMessageModel.h"
- #import "UIImage+NIMKit.h"
- #import "NIMKitAudioCenter.h"
- #import "NIMKit.h"
- @interface NIMSessionAudioContentView()<NIMMediaManagerDelegate>
- @property (nonatomic,strong) UIImageView *voiceImageView;
- @property (nonatomic,strong) UILabel *durationLabel;
- @end
- @implementation NIMSessionAudioContentView
- -(instancetype)initSessionMessageContentView{
- self = [super initSessionMessageContentView];
- if (self) {
- [self addVoiceView];
- [[NIMSDK sharedSDK].mediaManager addDelegate:self];
- }
- return self;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [[NIMSDK sharedSDK].mediaManager removeDelegate:self];
- }
- - (void)setPlaying:(BOOL)isPlaying
- {
- if (isPlaying) {
- [self.voiceImageView startAnimating];
- }else{
- [self.voiceImageView stopAnimating];
- }
- }
- - (void)addVoiceView{
- UIImage * image = [UIImage nim_imageInKit:@"icon_receiver_voice_playing.png"];
- _voiceImageView = [[UIImageView alloc] initWithImage:image];
- NSArray * animateNames = @[@"icon_receiver_voice_playing_001.png",@"icon_receiver_voice_playing_002.png",@"icon_receiver_voice_playing_003.png"];
- NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
- for (NSString * animateName in animateNames) {
- UIImage * animateImage = [UIImage nim_imageInKit:animateName];
- [animationImages addObject:animateImage];
- }
- _voiceImageView.animationImages = animationImages;
- _voiceImageView.animationDuration = 1.0;
- [self addSubview:_voiceImageView];
-
- _durationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
- _durationLabel.backgroundColor = [UIColor clearColor];
- [self addSubview:_durationLabel];
- }
- - (void)refresh:(NIMMessageModel *)data{
- [super refresh:data];
- NIMAudioObject *object = self.model.message.messageObject;
- if (self.model.message.isOutgoingMsg) {
- _voiceImageView.image = [UIImage imageNamed:@"vqu_images_IM_voice_white_3.png"];
- NSArray * animateNames = @[@"vqu_images_IM_voice_white_1.png",@"vqu_images_IM_voice_white_2.png",@"vqu_images_IM_voice_white_3.png"];
- NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
- for (NSString * animateName in animateNames) {
- UIImage * animateImage = [UIImage nim_imageInKit:animateName];
- [animationImages addObject:animateImage];
- }
- _voiceImageView.animationImages = animationImages;
- _voiceImageView.animationDuration = 1.0;
-
- }else{
- _voiceImageView.image = [UIImage imageNamed:@"vqu_images_IM_im_left_3"];
- NSArray * animateNames = @[@"vqu_images_IM_im_left_1",@"vqu_images_IM_im_left_2",@"vqu_images_IM_im_left_3"];
- NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
- for (NSString * animateName in animateNames) {
- UIImage * animateImage = [UIImage nim_imageInKit:animateName];
- [animationImages addObject:animateImage];
- }
- _voiceImageView.animationImages = animationImages;
- _voiceImageView.animationDuration = 1.0;
-
- }
- self.durationLabel.text = [NSString stringWithFormat:@"%zd\"",(object.duration+500)/1000];//四舍五入
-
- NIMKitSetting *setting = [[NIMKit sharedKit].config setting:data.message];
- self.durationLabel.font = setting.font;
- self.durationLabel.textColor = setting.textColor;
-
- [self.durationLabel sizeToFit];
-
- [self setPlaying:self.isPlaying];
- }
- - (void)layoutSubviews{
- [super layoutSubviews];
- UIEdgeInsets contentInsets = self.model.contentViewInsets;
- if (self.model.message.isOutgoingMsg) {
- self.voiceImageView.nim_right = self.nim_width - contentInsets.right;
- _durationLabel.nim_left = contentInsets.left;
- } else
- {
- self.voiceImageView.nim_left = contentInsets.left;
- _durationLabel.nim_right = self.nim_width - contentInsets.right;
- }
- _voiceImageView.nim_centerY = self.nim_height * .5f;
- _durationLabel.nim_centerY = _voiceImageView.nim_centerY;
- }
- -(void)onTouchUpInside:(id)sender
- {
- if ([self.model.message attachmentDownloadState]== NIMMessageAttachmentDownloadStateFailed) {
- if (self.audioUIDelegate && [self.audioUIDelegate respondsToSelector:@selector(retryDownloadMsg)]) {
- [self.audioUIDelegate retryDownloadMsg];
- }
- return;
- }
- if ([self.model.message attachmentDownloadState] == NIMMessageAttachmentDownloadStateDownloaded) {
-
- if ([[NIMSDK sharedSDK].mediaManager isPlaying]) {
- [self stopPlayingUI];
- }
-
- NIMKitEvent *event = [[NIMKitEvent alloc] init];
- event.eventName = NIMKitEventNameTapAudio;
- event.messageModel = self.model;
- [self.delegate onCatchEvent:event];
- }
- }
- #pragma mark - NIMMediaManagerDelegate
- - (void)playAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
- if(filePath && !error) {
- if (self.isPlaying && [self.audioUIDelegate respondsToSelector:@selector(startPlayingAudioUI)]) {
- [self.audioUIDelegate startPlayingAudioUI];
- }
- }
- }
- - (void)playAudio:(NSString *)filePath didCompletedWithError:(NSError *)error
- {
- [self stopPlayingUI];
- }
- #pragma mark - private methods
- - (void)stopPlayingUI
- {
- [self setPlaying:NO];
- }
- - (BOOL)isPlaying
- {
- return [NIMKitAudioCenter instance].currentPlayingMessage == self.model.message; //对比是否是同一条消息,严格同一条,不能是相同ID,防止进了会话又进云端消息界面,导致同一个ID的云消息也在动画
- }
- @end
|