NIMSessionAudioContentView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // SessionAudioCententView.m
  3. // NIMDemo
  4. //
  5. // Created by chris.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMSessionAudioContentView.h"
  9. #import "UIView+NIM.h"
  10. #import "NIMMessageModel.h"
  11. #import "UIImage+NIMKit.h"
  12. #import "NIMKitAudioCenter.h"
  13. #import "NIMKit.h"
  14. @interface NIMSessionAudioContentView()<NIMMediaManagerDelegate>
  15. @property (nonatomic,strong) UIImageView *voiceImageView;
  16. @property (nonatomic,strong) UILabel *durationLabel;
  17. @end
  18. @implementation NIMSessionAudioContentView
  19. -(instancetype)initSessionMessageContentView{
  20. self = [super initSessionMessageContentView];
  21. if (self) {
  22. [self addVoiceView];
  23. [[NIMSDK sharedSDK].mediaManager addDelegate:self];
  24. }
  25. return self;
  26. }
  27. - (void)dealloc
  28. {
  29. [[NSNotificationCenter defaultCenter] removeObserver:self];
  30. [[NIMSDK sharedSDK].mediaManager removeDelegate:self];
  31. }
  32. - (void)setPlaying:(BOOL)isPlaying
  33. {
  34. if (isPlaying) {
  35. [self.voiceImageView startAnimating];
  36. }else{
  37. [self.voiceImageView stopAnimating];
  38. }
  39. }
  40. - (void)addVoiceView{
  41. UIImage * image = [UIImage nim_imageInKit:@"icon_receiver_voice_playing.png"];
  42. _voiceImageView = [[UIImageView alloc] initWithImage:image];
  43. NSArray * animateNames = @[@"icon_receiver_voice_playing_001.png",@"icon_receiver_voice_playing_002.png",@"icon_receiver_voice_playing_003.png"];
  44. NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
  45. for (NSString * animateName in animateNames) {
  46. UIImage * animateImage = [UIImage nim_imageInKit:animateName];
  47. [animationImages addObject:animateImage];
  48. }
  49. _voiceImageView.animationImages = animationImages;
  50. _voiceImageView.animationDuration = 1.0;
  51. [self addSubview:_voiceImageView];
  52. _durationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  53. _durationLabel.backgroundColor = [UIColor clearColor];
  54. [self addSubview:_durationLabel];
  55. }
  56. - (void)refresh:(NIMMessageModel *)data{
  57. [super refresh:data];
  58. NIMAudioObject *object = self.model.message.messageObject;
  59. if (self.model.message.isOutgoingMsg) {
  60. _voiceImageView.image = [UIImage imageNamed:@"vqu_images_IM_voice_white_3.png"];
  61. NSArray * animateNames = @[@"vqu_images_IM_voice_white_1.png",@"vqu_images_IM_voice_white_2.png",@"vqu_images_IM_voice_white_3.png"];
  62. NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
  63. for (NSString * animateName in animateNames) {
  64. UIImage * animateImage = [UIImage nim_imageInKit:animateName];
  65. [animationImages addObject:animateImage];
  66. }
  67. _voiceImageView.animationImages = animationImages;
  68. _voiceImageView.animationDuration = 1.0;
  69. }else{
  70. _voiceImageView.image = [UIImage imageNamed:@"vqu_images_IM_im_left_3"];
  71. NSArray * animateNames = @[@"vqu_images_IM_im_left_1",@"vqu_images_IM_im_left_2",@"vqu_images_IM_im_left_3"];
  72. NSMutableArray * animationImages = [[NSMutableArray alloc] initWithCapacity:animateNames.count];
  73. for (NSString * animateName in animateNames) {
  74. UIImage * animateImage = [UIImage nim_imageInKit:animateName];
  75. [animationImages addObject:animateImage];
  76. }
  77. _voiceImageView.animationImages = animationImages;
  78. _voiceImageView.animationDuration = 1.0;
  79. }
  80. self.durationLabel.text = [NSString stringWithFormat:@"%zd\"",(object.duration+500)/1000];//四舍五入
  81. NIMKitSetting *setting = [[NIMKit sharedKit].config setting:data.message];
  82. self.durationLabel.font = setting.font;
  83. self.durationLabel.textColor = setting.textColor;
  84. [self.durationLabel sizeToFit];
  85. [self setPlaying:self.isPlaying];
  86. }
  87. - (void)layoutSubviews{
  88. [super layoutSubviews];
  89. UIEdgeInsets contentInsets = self.model.contentViewInsets;
  90. if (self.model.message.isOutgoingMsg) {
  91. self.voiceImageView.nim_right = self.nim_width - contentInsets.right;
  92. _durationLabel.nim_left = contentInsets.left;
  93. } else
  94. {
  95. self.voiceImageView.nim_left = contentInsets.left;
  96. _durationLabel.nim_right = self.nim_width - contentInsets.right;
  97. }
  98. _voiceImageView.nim_centerY = self.nim_height * .5f;
  99. _durationLabel.nim_centerY = _voiceImageView.nim_centerY;
  100. }
  101. -(void)onTouchUpInside:(id)sender
  102. {
  103. if ([self.model.message attachmentDownloadState]== NIMMessageAttachmentDownloadStateFailed) {
  104. if (self.audioUIDelegate && [self.audioUIDelegate respondsToSelector:@selector(retryDownloadMsg)]) {
  105. [self.audioUIDelegate retryDownloadMsg];
  106. }
  107. return;
  108. }
  109. if ([self.model.message attachmentDownloadState] == NIMMessageAttachmentDownloadStateDownloaded) {
  110. if ([[NIMSDK sharedSDK].mediaManager isPlaying]) {
  111. [self stopPlayingUI];
  112. }
  113. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  114. event.eventName = NIMKitEventNameTapAudio;
  115. event.messageModel = self.model;
  116. [self.delegate onCatchEvent:event];
  117. }
  118. }
  119. #pragma mark - NIMMediaManagerDelegate
  120. - (void)playAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
  121. if(filePath && !error) {
  122. if (self.isPlaying && [self.audioUIDelegate respondsToSelector:@selector(startPlayingAudioUI)]) {
  123. [self.audioUIDelegate startPlayingAudioUI];
  124. }
  125. }
  126. }
  127. - (void)playAudio:(NSString *)filePath didCompletedWithError:(NSError *)error
  128. {
  129. [self stopPlayingUI];
  130. }
  131. #pragma mark - private methods
  132. - (void)stopPlayingUI
  133. {
  134. [self setPlaying:NO];
  135. }
  136. - (BOOL)isPlaying
  137. {
  138. return [NIMKitAudioCenter instance].currentPlayingMessage == self.model.message; //对比是否是同一条消息,严格同一条,不能是相同ID,防止进了会话又进云端消息界面,导致同一个ID的云消息也在动画
  139. }
  140. @end