Commit 3c969894c61da03430820d4ad893ce130859dd82
1 parent
4e70cf52
优化
Showing
7 changed files
with
301 additions
and
83 deletions
CNLiveChatSDKDemo/CNLiveChatSDKDemo/Assets.xcassets/fanhui.imageset/Contents.json
0 → 100644
| 1 | +{ | ||
| 2 | + "images" : [ | ||
| 3 | + { | ||
| 4 | + "idiom" : "universal", | ||
| 5 | + "scale" : "1x" | ||
| 6 | + }, | ||
| 7 | + { | ||
| 8 | + "idiom" : "universal", | ||
| 9 | + "filename" : "fanhui.png", | ||
| 10 | + "scale" : "2x" | ||
| 11 | + }, | ||
| 12 | + { | ||
| 13 | + "idiom" : "universal", | ||
| 14 | + "scale" : "3x" | ||
| 15 | + } | ||
| 16 | + ], | ||
| 17 | + "info" : { | ||
| 18 | + "version" : 1, | ||
| 19 | + "author" : "xcode" | ||
| 20 | + } | ||
| 21 | +} | ||
| 0 | \ No newline at end of file | 22 | \ No newline at end of file |
CNLiveChatSDKDemo/CNLiveChatSDKDemo/Assets.xcassets/fanhui.imageset/fanhui.png
0 → 100644
1.57 KB
CNLiveChatSDKDemo/CNLiveChatSDKDemo/CNLiveVoiceTableViewCell.h
| @@ -10,6 +10,10 @@ | @@ -10,6 +10,10 @@ | ||
| 10 | 10 | ||
| 11 | @interface CNLiveVoiceTableViewCell : UITableViewCell | 11 | @interface CNLiveVoiceTableViewCell : UITableViewCell |
| 12 | 12 | ||
| 13 | -@property (nonatomic, strong) MessageModel *model; | 13 | +@property (nonatomic, strong) NSIndexPath *indexPath; |
| 14 | + | ||
| 15 | +@property (nonatomic, strong) UIButton *voicePlayBtn; | ||
| 16 | + | ||
| 17 | +@property (nonatomic, strong) UILabel *timeLabel; | ||
| 14 | 18 | ||
| 15 | @end | 19 | @end |
CNLiveChatSDKDemo/CNLiveChatSDKDemo/CNLiveVoiceTableViewCell.m
| @@ -8,18 +8,7 @@ | @@ -8,18 +8,7 @@ | ||
| 8 | 8 | ||
| 9 | #import "CNLiveVoiceTableViewCell.h" | 9 | #import "CNLiveVoiceTableViewCell.h" |
| 10 | 10 | ||
| 11 | -@interface CNLiveVoiceTableViewCell()<AVAudioPlayerDelegate> { | ||
| 12 | - | ||
| 13 | - BOOL _isPlaying; | ||
| 14 | - NSTimer *_voiceTimer; | ||
| 15 | - long _voiceTime; | ||
| 16 | -} | ||
| 17 | - | ||
| 18 | -@property (nonatomic, strong) UIButton *voicePlayBtn; | ||
| 19 | - | ||
| 20 | -@property (nonatomic, strong) UILabel *timeLabel; | ||
| 21 | - | ||
| 22 | -@property (nonatomic, strong) AVAudioPlayer *player; | 11 | +@interface CNLiveVoiceTableViewCell() |
| 23 | 12 | ||
| 24 | @end | 13 | @end |
| 25 | 14 | ||
| @@ -34,63 +23,12 @@ | @@ -34,63 +23,12 @@ | ||
| 34 | return self; | 23 | return self; |
| 35 | } | 24 | } |
| 36 | 25 | ||
| 37 | -- (void)setModel:(MessageModel *)model { | ||
| 38 | - if (_model != model) { | ||
| 39 | - _model = model; | ||
| 40 | - } | ||
| 41 | - _voiceTime = model.duration; | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -- (void)timeCountDown { | ||
| 45 | - | ||
| 46 | - if (_voiceTime > 0) { | ||
| 47 | - _voiceTime = _voiceTime - 1; | ||
| 48 | - NSLog(@"%ld",_voiceTime); | ||
| 49 | - self.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 50 | - } else { | ||
| 51 | - [_player stop]; | ||
| 52 | - _player = nil; | ||
| 53 | - _isPlaying = NO; | ||
| 54 | - [_voiceTimer invalidate]; | ||
| 55 | - _voiceTimer = nil; | ||
| 56 | - [self.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 57 | - [self.timeLabel removeFromSuperview]; | ||
| 58 | - _voiceTime = _model.duration; | ||
| 59 | - } | ||
| 60 | -} | ||
| 61 | - | ||
| 62 | -- (void)voicePlay { | ||
| 63 | - | ||
| 64 | - if (_isPlaying) { | ||
| 65 | - [_player stop]; | ||
| 66 | - _player = nil; | ||
| 67 | - [self.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 68 | - } else { | ||
| 69 | - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; | ||
| 70 | - NSError *error=nil; | ||
| 71 | - _player = [[AVAudioPlayer alloc] initWithData:_model.wavAudioData error:&error]; | ||
| 72 | - _player.delegate = self; | ||
| 73 | - _player.numberOfLoops = 0; | ||
| 74 | - [_player prepareToPlay]; | ||
| 75 | - if (error) { | ||
| 76 | - NSLog(@"初始化播放器过程发生错误,错误信息:%@",error.localizedDescription); | ||
| 77 | - } | ||
| 78 | - [_player play]; | ||
| 79 | - [self.voicePlayBtn setImage:[UIImage imageNamed:@"语音播"] forState:UIControlStateNormal]; | ||
| 80 | - [self.contentView addSubview:self.timeLabel]; | ||
| 81 | - self.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 82 | - _voiceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeCountDown) userInfo:nil repeats:YES]; | ||
| 83 | - } | ||
| 84 | - _isPlaying = !_isPlaying; | ||
| 85 | -} | ||
| 86 | - | ||
| 87 | - (UIButton *)voicePlayBtn { | 26 | - (UIButton *)voicePlayBtn { |
| 88 | if (!_voicePlayBtn) { | 27 | if (!_voicePlayBtn) { |
| 89 | _voicePlayBtn = [UIButton buttonWithType:UIButtonTypeCustom]; | 28 | _voicePlayBtn = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 90 | [_voicePlayBtn setFrame:CGRectMake(10, 5, 100, 40)]; | 29 | [_voicePlayBtn setFrame:CGRectMake(10, 5, 100, 40)]; |
| 91 | [_voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | 30 | [_voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; |
| 92 | _voicePlayBtn.backgroundColor = [UIColor lightGrayColor]; | 31 | _voicePlayBtn.backgroundColor = [UIColor lightGrayColor]; |
| 93 | - [_voicePlayBtn addTarget:self action:@selector(voicePlay) forControlEvents:UIControlEventTouchUpInside]; | ||
| 94 | } | 32 | } |
| 95 | return _voicePlayBtn; | 33 | return _voicePlayBtn; |
| 96 | } | 34 | } |
CNLiveChatSDKDemo/CNLiveChatSDKDemo/ChatRoomViewController.m
| @@ -14,11 +14,15 @@ static NSString *imageCellId = @"imageCellId"; | @@ -14,11 +14,15 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 14 | 14 | ||
| 15 | #define chatRoomId @"67_78734255" | 15 | #define chatRoomId @"67_78734255" |
| 16 | 16 | ||
| 17 | -@interface ChatRoomViewController ()<UITextFieldDelegate, CNLiveChatManagerDelegate, UITableViewDelegate, UITableViewDataSource> { | 17 | +@interface ChatRoomViewController ()<UITextFieldDelegate, CNLiveChatManagerDelegate, UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> { |
| 18 | 18 | ||
| 19 | NSMutableArray *_messageArrayM; | 19 | NSMutableArray *_messageArrayM; |
| 20 | BOOL _isVoice; | 20 | BOOL _isVoice; |
| 21 | NSTimer *_getCountTimer; | 21 | NSTimer *_getCountTimer; |
| 22 | + NSTimer *_voiceTimer; | ||
| 23 | + BOOL _isPlaying; | ||
| 24 | + long _voiceTime; | ||
| 25 | + CNLiveVoiceTableViewCell *_preCell; | ||
| 22 | } | 26 | } |
| 23 | 27 | ||
| 24 | @property (nonatomic, strong) UIView *textViewBackView; | 28 | @property (nonatomic, strong) UIView *textViewBackView; |
| @@ -40,6 +44,8 @@ static NSString *imageCellId = @"imageCellId"; | @@ -40,6 +44,8 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 40 | @property (nonatomic, strong) NSData *recordedData; // 录音数据 | 44 | @property (nonatomic, strong) NSData *recordedData; // 录音数据 |
| 41 | @property (nonatomic, assign) NSTimeInterval duration; // 录音时长 | 45 | @property (nonatomic, assign) NSTimeInterval duration; // 录音时长 |
| 42 | 46 | ||
| 47 | +@property (nonatomic, strong) AVAudioPlayer *player; | ||
| 48 | + | ||
| 43 | @end | 49 | @end |
| 44 | 50 | ||
| 45 | @implementation ChatRoomViewController | 51 | @implementation ChatRoomViewController |
| @@ -49,6 +55,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -49,6 +55,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 49 | 55 | ||
| 50 | self.view.backgroundColor = [UIColor whiteColor]; | 56 | self.view.backgroundColor = [UIColor whiteColor]; |
| 51 | self.title = @"聊天室"; | 57 | self.title = @"聊天室"; |
| 58 | + [self createBackButton]; | ||
| 52 | [self addNotification]; | 59 | [self addNotification]; |
| 53 | self.automaticallyAdjustsScrollViewInsets = NO; | 60 | self.automaticallyAdjustsScrollViewInsets = NO; |
| 54 | 61 | ||
| @@ -78,21 +85,35 @@ static NSString *imageCellId = @"imageCellId"; | @@ -78,21 +85,35 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 78 | _messageArrayM = [[NSMutableArray alloc] init]; | 85 | _messageArrayM = [[NSMutableArray alloc] init]; |
| 79 | } | 86 | } |
| 80 | 87 | ||
| 81 | -- (void)viewWillDisappear:(BOOL)animated { | ||
| 82 | - | ||
| 83 | - [super viewWillDisappear:animated]; | 88 | +- (void)createBackButton { |
| 89 | + self.navigationItem.hidesBackButton = YES; | ||
| 90 | + UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)]; | ||
| 91 | + [button setImage:[[UIImage imageNamed:@"fanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; | ||
| 92 | + [button addTarget:self action:@selector(quitChatRoom) forControlEvents:UIControlEventTouchUpInside]; | ||
| 93 | + UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button]; | ||
| 94 | + UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] | ||
| 95 | + initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace | ||
| 96 | + target:nil action:nil]; | ||
| 97 | + negativeSpacer.width = -15; | ||
| 98 | + self.navigationItem.leftBarButtonItems = @[negativeSpacer, backItem]; | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +- (void)quitChatRoom { | ||
| 84 | 102 | ||
| 85 | [_getCountTimer invalidate]; | 103 | [_getCountTimer invalidate]; |
| 86 | _getCountTimer = nil; | 104 | _getCountTimer = nil; |
| 105 | + [_voiceTimer invalidate]; | ||
| 106 | + _voiceTimer = nil; | ||
| 87 | 107 | ||
| 88 | [[CNLiveChatManager sharedCNLiveChatManager] quitChatRoom:chatRoomId success:^(NSString *targetId) { | 108 | [[CNLiveChatManager sharedCNLiveChatManager] quitChatRoom:chatRoomId success:^(NSString *targetId) { |
| 89 | NSLog(@"退出聊天室:%@成功",targetId); | 109 | NSLog(@"退出聊天室:%@成功",targetId); |
| 90 | } error:^(CNLiveChatErrorCode errors, NSString *targetId) { | 110 | } error:^(CNLiveChatErrorCode errors, NSString *targetId) { |
| 91 | NSLog(@"退出聊天室:%@失败,错误码:%ld",targetId,(long)errors); | 111 | NSLog(@"退出聊天室:%@失败,错误码:%ld",targetId,(long)errors); |
| 92 | }]; | 112 | }]; |
| 113 | + | ||
| 114 | + [self.navigationController popViewControllerAnimated:YES]; | ||
| 93 | } | 115 | } |
| 94 | 116 | ||
| 95 | - | ||
| 96 | #pragma mark - 获取聊天室人数 | 117 | #pragma mark - 获取聊天室人数 |
| 97 | - (void)getChatRoomPersonCount { | 118 | - (void)getChatRoomPersonCount { |
| 98 | 119 | ||
| @@ -138,7 +159,8 @@ static NSString *imageCellId = @"imageCellId"; | @@ -138,7 +159,8 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 138 | } else if (model.wavAudioData) { // 语音消息 | 159 | } else if (model.wavAudioData) { // 语音消息 |
| 139 | 160 | ||
| 140 | CNLiveVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:voiceCellId forIndexPath:indexPath]; | 161 | CNLiveVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:voiceCellId forIndexPath:indexPath]; |
| 141 | - cell.model = model; | 162 | + [cell.voicePlayBtn addTarget:self action:@selector(voicePlay:) forControlEvents:UIControlEventTouchUpInside]; |
| 163 | + cell.indexPath = indexPath; | ||
| 142 | return cell; | 164 | return cell; |
| 143 | } else if (model.wavAudioData || model.thumbnailImage) { | 165 | } else if (model.wavAudioData || model.thumbnailImage) { |
| 144 | 166 | ||
| @@ -278,10 +300,44 @@ static NSString *imageCellId = @"imageCellId"; | @@ -278,10 +300,44 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 278 | } | 300 | } |
| 279 | 301 | ||
| 280 | #pragma mark - 图片 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | 302 | #pragma mark - 图片 Button Action !!!!!!!!!!!!!!!!!!!!!!!! |
| 281 | -- (void)sendImage { | 303 | +- (void)getImageFromIpc |
| 304 | +{ | ||
| 305 | + // 1.判断相册是否可以打开 | ||
| 306 | + if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; | ||
| 307 | + // 2. 创建图片选择控制器 | ||
| 308 | + UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; | ||
| 309 | + /** | ||
| 310 | + typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { | ||
| 311 | + UIImagePickerControllerSourceTypePhotoLibrary, // 相册 | ||
| 312 | + UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取 | ||
| 313 | + UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿 | ||
| 314 | + } | ||
| 315 | + */ | ||
| 316 | + // 3. 设置打开照片相册类型(显示所有相簿) | ||
| 317 | + ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | ||
| 318 | + // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; | ||
| 319 | + // ipc.sourceType = UIImagePickerControllerSourceTypeCamera; | ||
| 282 | 320 | ||
| 283 | - NSString *path = [[NSBundle mainBundle] pathForResource:@"tu.jpg" ofType:nil]; | ||
| 284 | - CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImageURI:path]; | 321 | + ipc.delegate = self; |
| 322 | + | ||
| 323 | + [self presentViewController:ipc animated:YES completion:nil]; | ||
| 324 | +} | ||
| 325 | + | ||
| 326 | +#pragma mark -- <UIImagePickerControllerDelegate>-- | ||
| 327 | +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { // 获取图片后的操作 | ||
| 328 | + | ||
| 329 | + [picker dismissViewControllerAnimated:YES completion:nil]; | ||
| 330 | + | ||
| 331 | + // 设置图片 | ||
| 332 | + UIImage *img = info[UIImagePickerControllerOriginalImage]; | ||
| 333 | + [self sendImage:img]; | ||
| 334 | +} | ||
| 335 | + | ||
| 336 | +- (void)sendImage:(UIImage *)image { | ||
| 337 | + | ||
| 338 | +// NSString *path = [[NSBundle mainBundle] pathForResource:@"tu.jpg" ofType:nil]; | ||
| 339 | +// CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImageURI:path]; | ||
| 340 | + CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImage:image]; | ||
| 285 | 341 | ||
| 286 | [[CNLiveChatManager sharedCNLiveChatManager] sendMediaMessage:CNLive_ConversationType_CHATROOM targetId:chatRoomId content:imageMsg progress:^(int progress, NSString *messageId) { | 342 | [[CNLiveChatManager sharedCNLiveChatManager] sendMediaMessage:CNLive_ConversationType_CHATROOM targetId:chatRoomId content:imageMsg progress:^(int progress, NSString *messageId) { |
| 287 | NSLog(@"发送进度"); | 343 | NSLog(@"发送进度"); |
| @@ -312,7 +368,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -312,7 +368,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 312 | }]; | 368 | }]; |
| 313 | } | 369 | } |
| 314 | 370 | ||
| 315 | -#pragma mark - 语音 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | 371 | +#pragma mark - 语音 录音 Button Action !!!!!!!!!!!!!!!!!!!!!!!! |
| 316 | - (void)voiceAction { | 372 | - (void)voiceAction { |
| 317 | 373 | ||
| 318 | _isVoice = !_isVoice; | 374 | _isVoice = !_isVoice; |
| @@ -385,6 +441,73 @@ static NSString *imageCellId = @"imageCellId"; | @@ -385,6 +441,73 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 385 | } | 441 | } |
| 386 | } | 442 | } |
| 387 | 443 | ||
| 444 | +#pragma mark - 语音 播放 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | ||
| 445 | +- (void)voicePlay:(UIButton *)button { | ||
| 446 | + | ||
| 447 | + if (_isPlaying) { | ||
| 448 | + [_player stop]; | ||
| 449 | + _player = nil; | ||
| 450 | + _isPlaying = NO; | ||
| 451 | + [_voiceTimer invalidate]; | ||
| 452 | + _voiceTimer = nil; | ||
| 453 | + [_preCell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 454 | + [_preCell.timeLabel removeFromSuperview]; | ||
| 455 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)[[button superview] superview]; | ||
| 456 | + if (_preCell == cell) { | ||
| 457 | + return; | ||
| 458 | + } else { | ||
| 459 | + [self voicePlayStart:button]; | ||
| 460 | + } | ||
| 461 | + } else { | ||
| 462 | + [self voicePlayStart:button]; | ||
| 463 | + } | ||
| 464 | + _isPlaying = !_isPlaying; | ||
| 465 | +} | ||
| 466 | + | ||
| 467 | +- (void)voicePlayStart:(UIButton *)button { | ||
| 468 | + | ||
| 469 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)[[button superview] superview]; | ||
| 470 | + _preCell = cell; | ||
| 471 | + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; | ||
| 472 | + NSError *error=nil; | ||
| 473 | + MessageModel *model = _messageArrayM[cell.indexPath.row]; | ||
| 474 | + _voiceTime = model.duration; | ||
| 475 | + _player = [[AVAudioPlayer alloc] initWithData:model.wavAudioData error:&error]; | ||
| 476 | + _player.delegate = self; | ||
| 477 | + _player.numberOfLoops = 0; | ||
| 478 | + [_player prepareToPlay]; | ||
| 479 | + if (error) { | ||
| 480 | + NSLog(@"初始化播放器过程发生错误,错误信息:%@",error.localizedDescription); | ||
| 481 | + } | ||
| 482 | + [_player play]; | ||
| 483 | + [cell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播"] forState:UIControlStateNormal]; | ||
| 484 | + [cell.contentView addSubview:cell.timeLabel]; | ||
| 485 | + cell.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 486 | + NSDictionary *dict = @{@"cell":cell, | ||
| 487 | + @"model":model}; | ||
| 488 | + _voiceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeCountDown:) userInfo:dict repeats:YES]; | ||
| 489 | +} | ||
| 490 | + | ||
| 491 | +- (void)timeCountDown:(NSTimer *)timer { | ||
| 492 | + | ||
| 493 | + NSDictionary *dict = [timer userInfo]; | ||
| 494 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)dict[@"cell"]; | ||
| 495 | + MessageModel *model = dict[@"model"]; | ||
| 496 | + if (_voiceTime > 0) { | ||
| 497 | + _voiceTime = _voiceTime - 1; | ||
| 498 | + cell.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 499 | + } else { | ||
| 500 | + [_player stop]; | ||
| 501 | + _player = nil; | ||
| 502 | + _isPlaying = NO; | ||
| 503 | + [_voiceTimer invalidate]; | ||
| 504 | + _voiceTimer = nil; | ||
| 505 | + [cell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 506 | + [cell.timeLabel removeFromSuperview]; | ||
| 507 | + _voiceTime = model.duration; | ||
| 508 | + } | ||
| 509 | +} | ||
| 510 | + | ||
| 388 | #pragma mark - Get Method !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | 511 | #pragma mark - Get Method !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 389 | - (UIView *)textViewBackView { | 512 | - (UIView *)textViewBackView { |
| 390 | if (!_textViewBackView) { | 513 | if (!_textViewBackView) { |
| @@ -432,7 +555,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -432,7 +555,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 432 | _imgButton = [UIButton buttonWithType:UIButtonTypeCustom]; | 555 | _imgButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 433 | _imgButton.frame = CGRectMake(KScreenWidth - 40, 5, 40, 40); | 556 | _imgButton.frame = CGRectMake(KScreenWidth - 40, 5, 40, 40); |
| 434 | [_imgButton setImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal]; | 557 | [_imgButton setImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal]; |
| 435 | - [_imgButton addTarget:self action:@selector(sendImage) forControlEvents:UIControlEventTouchUpInside]; | 558 | + [_imgButton addTarget:self action:@selector(getImageFromIpc) forControlEvents:UIControlEventTouchUpInside]; |
| 436 | } | 559 | } |
| 437 | return _imgButton; | 560 | return _imgButton; |
| 438 | } | 561 | } |
CNLiveChatSDKDemo/CNLiveChatSDKDemo/Info.plist
| @@ -2,6 +2,8 @@ | @@ -2,6 +2,8 @@ | ||
| 2 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | 2 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 3 | <plist version="1.0"> | 3 | <plist version="1.0"> |
| 4 | <dict> | 4 | <dict> |
| 5 | + <key>NSPhotoLibraryUsageDescription</key> | ||
| 6 | + <string>请求打开相册</string> | ||
| 5 | <key>CFBundleDevelopmentRegion</key> | 7 | <key>CFBundleDevelopmentRegion</key> |
| 6 | <string>en</string> | 8 | <string>en</string> |
| 7 | <key>CFBundleExecutable</key> | 9 | <key>CFBundleExecutable</key> |
| @@ -28,7 +30,7 @@ | @@ -28,7 +30,7 @@ | ||
| 28 | <key>NSCameraUsageDescription</key> | 30 | <key>NSCameraUsageDescription</key> |
| 29 | <string></string> | 31 | <string></string> |
| 30 | <key>NSMicrophoneUsageDescription</key> | 32 | <key>NSMicrophoneUsageDescription</key> |
| 31 | - <string></string> | 33 | + <string>请求打开话筒</string> |
| 32 | <key>UILaunchStoryboardName</key> | 34 | <key>UILaunchStoryboardName</key> |
| 33 | <string>LaunchScreen</string> | 35 | <string>LaunchScreen</string> |
| 34 | <key>UIMainStoryboardFile</key> | 36 | <key>UIMainStoryboardFile</key> |
CNLiveChatSDKDemo/CNLiveChatSDKDemo/PrivateViewController.m
| @@ -14,11 +14,15 @@ static NSString *textCellId = @"textCellId"; | @@ -14,11 +14,15 @@ static NSString *textCellId = @"textCellId"; | ||
| 14 | static NSString *voiceCellId = @"voiceCellId"; | 14 | static NSString *voiceCellId = @"voiceCellId"; |
| 15 | static NSString *imageCellId = @"imageCellId"; | 15 | static NSString *imageCellId = @"imageCellId"; |
| 16 | 16 | ||
| 17 | -@interface PrivateViewController ()<UITextFieldDelegate, CNLiveChatManagerDelegate, UITableViewDelegate, UITableViewDataSource> { | 17 | +@interface PrivateViewController ()<UITextFieldDelegate, CNLiveChatManagerDelegate, UITableViewDelegate, UITableViewDataSource, AVAudioPlayerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> { |
| 18 | 18 | ||
| 19 | NSMutableArray *_messageArrayM; | 19 | NSMutableArray *_messageArrayM; |
| 20 | BOOL _isVoice; | 20 | BOOL _isVoice; |
| 21 | NSTimer *_getCountTimer; | 21 | NSTimer *_getCountTimer; |
| 22 | + NSTimer *_voiceTimer; | ||
| 23 | + BOOL _isPlaying; | ||
| 24 | + long _voiceTime; | ||
| 25 | + CNLiveVoiceTableViewCell *_preCell; | ||
| 22 | } | 26 | } |
| 23 | 27 | ||
| 24 | @property (nonatomic, strong) UIView *textViewBackView; | 28 | @property (nonatomic, strong) UIView *textViewBackView; |
| @@ -38,6 +42,8 @@ static NSString *imageCellId = @"imageCellId"; | @@ -38,6 +42,8 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 38 | @property (nonatomic, strong) NSData *recordedData; // 录音数据 | 42 | @property (nonatomic, strong) NSData *recordedData; // 录音数据 |
| 39 | @property (nonatomic, assign) NSTimeInterval duration; // 录音时长 | 43 | @property (nonatomic, assign) NSTimeInterval duration; // 录音时长 |
| 40 | 44 | ||
| 45 | +@property (nonatomic, strong) AVAudioPlayer *player; | ||
| 46 | + | ||
| 41 | @end | 47 | @end |
| 42 | 48 | ||
| 43 | @implementation PrivateViewController | 49 | @implementation PrivateViewController |
| @@ -47,6 +53,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -47,6 +53,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 47 | 53 | ||
| 48 | self.view.backgroundColor = [UIColor whiteColor]; | 54 | self.view.backgroundColor = [UIColor whiteColor]; |
| 49 | self.title = @"单聊"; | 55 | self.title = @"单聊"; |
| 56 | + [self createBackButton]; | ||
| 50 | [self addNotification]; | 57 | [self addNotification]; |
| 51 | self.automaticallyAdjustsScrollViewInsets = NO; | 58 | self.automaticallyAdjustsScrollViewInsets = NO; |
| 52 | 59 | ||
| @@ -64,6 +71,27 @@ static NSString *imageCellId = @"imageCellId"; | @@ -64,6 +71,27 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 64 | [self getPrivateChattingRecords]; | 71 | [self getPrivateChattingRecords]; |
| 65 | } | 72 | } |
| 66 | 73 | ||
| 74 | +- (void)createBackButton { | ||
| 75 | + self.navigationItem.hidesBackButton = YES; | ||
| 76 | + UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)]; | ||
| 77 | + [button setImage:[[UIImage imageNamed:@"fanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; | ||
| 78 | + [button addTarget:self action:@selector(quitChatRoom) forControlEvents:UIControlEventTouchUpInside]; | ||
| 79 | + UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button]; | ||
| 80 | + UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] | ||
| 81 | + initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace | ||
| 82 | + target:nil action:nil]; | ||
| 83 | + negativeSpacer.width = -15; | ||
| 84 | + self.navigationItem.leftBarButtonItems = @[negativeSpacer, backItem]; | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +- (void)quitChatRoom { | ||
| 88 | + | ||
| 89 | + [_voiceTimer invalidate]; | ||
| 90 | + _voiceTimer = nil; | ||
| 91 | + | ||
| 92 | + [self.navigationController popViewControllerAnimated:YES]; | ||
| 93 | +} | ||
| 94 | + | ||
| 67 | #pragma mark - 获取私聊最新历史聊天记录 !!!!!!!!!!!!!!!!!!!!!!!!!!!! | 95 | #pragma mark - 获取私聊最新历史聊天记录 !!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 68 | - (void)getPrivateChattingRecords { | 96 | - (void)getPrivateChattingRecords { |
| 69 | 97 | ||
| @@ -153,7 +181,8 @@ static NSString *imageCellId = @"imageCellId"; | @@ -153,7 +181,8 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 153 | } else if (model.wavAudioData) { // 语音消息 | 181 | } else if (model.wavAudioData) { // 语音消息 |
| 154 | 182 | ||
| 155 | CNLiveVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:voiceCellId forIndexPath:indexPath]; | 183 | CNLiveVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:voiceCellId forIndexPath:indexPath]; |
| 156 | - cell.model = model; | 184 | + [cell.voicePlayBtn addTarget:self action:@selector(voicePlay:) forControlEvents:UIControlEventTouchUpInside]; |
| 185 | + cell.indexPath = indexPath; | ||
| 157 | return cell; | 186 | return cell; |
| 158 | } else if (model.wavAudioData || model.thumbnailImage) { // 图片消息 | 187 | } else if (model.wavAudioData || model.thumbnailImage) { // 图片消息 |
| 159 | 188 | ||
| @@ -273,10 +302,44 @@ static NSString *imageCellId = @"imageCellId"; | @@ -273,10 +302,44 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 273 | } | 302 | } |
| 274 | 303 | ||
| 275 | #pragma mark - 图片 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | 304 | #pragma mark - 图片 Button Action !!!!!!!!!!!!!!!!!!!!!!!! |
| 276 | -- (void)sendImage { | 305 | +- (void)getImageFromIpc |
| 306 | +{ | ||
| 307 | + // 1.判断相册是否可以打开 | ||
| 308 | + if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return; | ||
| 309 | + // 2. 创建图片选择控制器 | ||
| 310 | + UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; | ||
| 311 | + /** | ||
| 312 | + typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { | ||
| 313 | + UIImagePickerControllerSourceTypePhotoLibrary, // 相册 | ||
| 314 | + UIImagePickerControllerSourceTypeCamera, // 用相机拍摄获取 | ||
| 315 | + UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相簿 | ||
| 316 | + } | ||
| 317 | + */ | ||
| 318 | + // 3. 设置打开照片相册类型(显示所有相簿) | ||
| 319 | + ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | ||
| 320 | + // ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; | ||
| 321 | + // ipc.sourceType = UIImagePickerControllerSourceTypeCamera; | ||
| 322 | + | ||
| 323 | + ipc.delegate = self; | ||
| 324 | + | ||
| 325 | + [self presentViewController:ipc animated:YES completion:nil]; | ||
| 326 | +} | ||
| 327 | + | ||
| 328 | +#pragma mark -- <UIImagePickerControllerDelegate>-- | ||
| 329 | +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { // 获取图片后的操作 | ||
| 330 | + | ||
| 331 | + [picker dismissViewControllerAnimated:YES completion:nil]; | ||
| 332 | + | ||
| 333 | + // 设置图片 | ||
| 334 | + UIImage *img = info[UIImagePickerControllerOriginalImage]; | ||
| 335 | + [self sendImage:img]; | ||
| 336 | +} | ||
| 337 | + | ||
| 338 | +- (void)sendImage:(UIImage *)image { | ||
| 277 | 339 | ||
| 278 | - NSString *path = [[NSBundle mainBundle] pathForResource:@"tu.jpg" ofType:nil]; | ||
| 279 | - CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImageURI:path]; | 340 | + // NSString *path = [[NSBundle mainBundle] pathForResource:@"tu.jpg" ofType:nil]; |
| 341 | + // CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImageURI:path]; | ||
| 342 | + CNliveChatImageMessage *imageMsg = [CNliveChatImageMessage messageWithImage:image]; | ||
| 280 | 343 | ||
| 281 | [[CNLiveChatManager sharedCNLiveChatManager] sendMediaMessage:CNLive_ConversationType_PRIVATE targetId:privateUserId content:imageMsg progress:^(int progress, NSString *messageId) { | 344 | [[CNLiveChatManager sharedCNLiveChatManager] sendMediaMessage:CNLive_ConversationType_PRIVATE targetId:privateUserId content:imageMsg progress:^(int progress, NSString *messageId) { |
| 282 | NSLog(@"发送进度"); | 345 | NSLog(@"发送进度"); |
| @@ -307,7 +370,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -307,7 +370,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 307 | }]; | 370 | }]; |
| 308 | } | 371 | } |
| 309 | 372 | ||
| 310 | -#pragma mark - 语音 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | 373 | +#pragma mark - 语音 录制 Button Action !!!!!!!!!!!!!!!!!!!!!!!! |
| 311 | - (void)voiceAction { | 374 | - (void)voiceAction { |
| 312 | 375 | ||
| 313 | _isVoice = !_isVoice; | 376 | _isVoice = !_isVoice; |
| @@ -380,6 +443,73 @@ static NSString *imageCellId = @"imageCellId"; | @@ -380,6 +443,73 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 380 | } | 443 | } |
| 381 | } | 444 | } |
| 382 | 445 | ||
| 446 | +#pragma mark - 语音 播放 Button Action !!!!!!!!!!!!!!!!!!!!!!!! | ||
| 447 | +- (void)voicePlay:(UIButton *)button { | ||
| 448 | + | ||
| 449 | + if (_isPlaying) { | ||
| 450 | + [_player stop]; | ||
| 451 | + _player = nil; | ||
| 452 | + _isPlaying = NO; | ||
| 453 | + [_voiceTimer invalidate]; | ||
| 454 | + _voiceTimer = nil; | ||
| 455 | + [_preCell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 456 | + [_preCell.timeLabel removeFromSuperview]; | ||
| 457 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)[[button superview] superview]; | ||
| 458 | + if (_preCell == cell) { | ||
| 459 | + return; | ||
| 460 | + } else { | ||
| 461 | + [self voicePlayStart:button]; | ||
| 462 | + } | ||
| 463 | + } else { | ||
| 464 | + [self voicePlayStart:button]; | ||
| 465 | + } | ||
| 466 | + _isPlaying = !_isPlaying; | ||
| 467 | +} | ||
| 468 | + | ||
| 469 | +- (void)voicePlayStart:(UIButton *)button { | ||
| 470 | + | ||
| 471 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)[[button superview] superview]; | ||
| 472 | + _preCell = cell; | ||
| 473 | + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; | ||
| 474 | + NSError *error=nil; | ||
| 475 | + MessageModel *model = _messageArrayM[cell.indexPath.row]; | ||
| 476 | + _voiceTime = model.duration; | ||
| 477 | + _player = [[AVAudioPlayer alloc] initWithData:model.wavAudioData error:&error]; | ||
| 478 | + _player.delegate = self; | ||
| 479 | + _player.numberOfLoops = 0; | ||
| 480 | + [_player prepareToPlay]; | ||
| 481 | + if (error) { | ||
| 482 | + NSLog(@"初始化播放器过程发生错误,错误信息:%@",error.localizedDescription); | ||
| 483 | + } | ||
| 484 | + [_player play]; | ||
| 485 | + [cell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播"] forState:UIControlStateNormal]; | ||
| 486 | + [cell.contentView addSubview:cell.timeLabel]; | ||
| 487 | + cell.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 488 | + NSDictionary *dict = @{@"cell":cell, | ||
| 489 | + @"model":model}; | ||
| 490 | + _voiceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeCountDown:) userInfo:dict repeats:YES]; | ||
| 491 | +} | ||
| 492 | + | ||
| 493 | +- (void)timeCountDown:(NSTimer *)timer { | ||
| 494 | + | ||
| 495 | + NSDictionary *dict = [timer userInfo]; | ||
| 496 | + CNLiveVoiceTableViewCell *cell = (CNLiveVoiceTableViewCell *)dict[@"cell"]; | ||
| 497 | + MessageModel *model = dict[@"model"]; | ||
| 498 | + if (_voiceTime > 0) { | ||
| 499 | + _voiceTime = _voiceTime - 1; | ||
| 500 | + cell.timeLabel.text = [NSString stringWithFormat:@"%ld s",_voiceTime]; | ||
| 501 | + } else { | ||
| 502 | + [_player stop]; | ||
| 503 | + _player = nil; | ||
| 504 | + _isPlaying = NO; | ||
| 505 | + [_voiceTimer invalidate]; | ||
| 506 | + _voiceTimer = nil; | ||
| 507 | + [cell.voicePlayBtn setImage:[UIImage imageNamed:@"语音播放"] forState:UIControlStateNormal]; | ||
| 508 | + [cell.timeLabel removeFromSuperview]; | ||
| 509 | + _voiceTime = model.duration; | ||
| 510 | + } | ||
| 511 | +} | ||
| 512 | + | ||
| 383 | #pragma mark - Get Method !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | 513 | #pragma mark - Get Method !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 384 | - (UIView *)textViewBackView { | 514 | - (UIView *)textViewBackView { |
| 385 | if (!_textViewBackView) { | 515 | if (!_textViewBackView) { |
| @@ -419,7 +549,7 @@ static NSString *imageCellId = @"imageCellId"; | @@ -419,7 +549,7 @@ static NSString *imageCellId = @"imageCellId"; | ||
| 419 | _imgButton = [UIButton buttonWithType:UIButtonTypeCustom]; | 549 | _imgButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 420 | _imgButton.frame = CGRectMake(KScreenWidth - 40, 5, 40, 40); | 550 | _imgButton.frame = CGRectMake(KScreenWidth - 40, 5, 40, 40); |
| 421 | [_imgButton setImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal]; | 551 | [_imgButton setImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal]; |
| 422 | - [_imgButton addTarget:self action:@selector(sendImage) forControlEvents:UIControlEventTouchUpInside]; | 552 | + [_imgButton addTarget:self action:@selector(getImageFromIpc) forControlEvents:UIControlEventTouchUpInside]; |
| 423 | } | 553 | } |
| 424 | return _imgButton; | 554 | return _imgButton; |
| 425 | } | 555 | } |