局域网内端到端的聊天项目(七)

效果图:
iPhone.gif
iPod.gif
  • 上一篇已实现单个及多个视频的发送
  • 接下来实现 语音的录制/传输/播放功能
  • 同样通过键盘工具条来触发
  • 通过封装语音录制/播放工具类来获取资源
一.键盘工具条新增语音按钮
局域网内端到端的聊天项目(七)_第1张图片
IMG_2575(20171205-115040).jpg
// 新增三种状态
typedef NS_ENUM(NSInteger, RecordVoiceState)
{
    RecordVoiceStateBegin = 0,  // 开始
    RecordVoiceStateFinish = 1, // 结束
    RecordVoiceStateCancle = 2  // 取消
};

// 新增delegate回调
@protocol ESKeyBoardToolViewDelegate 
@optional
/// 录音 开始 结束
- (void)ESKeyBoardToolViewRecordWithState:(RecordVoiceState)state;
@end

// 新增语音/语音录制按钮
@interface ESKeyBoardToolView () 
/// 语音按钮
@property (nonatomic, strong) UIButton *voiceButton;
/// 开始录音按钮
@property (nonatomic, strong) UIButton *beginRecordButton;
@end

// 语音/开始录制按钮的监听
- (void)setupInit
{
    // 语音按钮
    self.voiceButton = [[UIButton alloc] init];
    [self.voiceButton setImage:[UIImage imageNamed:@"音频_bt"] forState:UIControlStateNormal];
    [self.voiceButton setImage:[UIImage imageNamed:@"文本_bt"] forState:UIControlStateSelected];
    [self.voiceButton addTarget:self action:@selector(voiceButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.voiceButton];
    
    // 开始录音按钮
    self.beginRecordButton = [[UIButton alloc] init];
    self.beginRecordButton.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:232.0/255.0 blue:238.0/255.0 alpha:0.5];
    [self.beginRecordButton setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self.beginRecordButton setTitle:@"松开 结束" forState:UIControlStateHighlighted];
    [self.beginRecordButton setBackgroundImage:[UIImage imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
    [self.beginRecordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.beginRecordButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [self.beginRecordButton addTarget:self action:@selector(recordButtonDidBegin:) forControlEvents:UIControlEventTouchDown];
    [self.beginRecordButton addTarget:self action:@selector(recordButtonDidFinish:) forControlEvents:UIControlEventTouchUpInside];
    [self.beginRecordButton addTarget:self action:@selector(recordButtonDidCancle:) forControlEvents:UIControlEventTouchUpOutside];
    [self addSubview:self.beginRecordButton];
    self.beginRecordButton.hidden = YES;
}
#pragma mark - event
// 语音按钮
- (void)voiceButtonDidClick:(UIButton *)button{
    button.selected = !button.selected;
    self.beginRecordButton.hidden = !button.selected;
    if (button.selected) {
        [self.inputTextView resignFirstResponder];
        [self exitKeyBoardInputView];
    }
}
// 开始录音
- (void)recordButtonDidBegin:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ESKeyBoardToolViewRecordWithState:)]) {
        [self.delegate ESKeyBoardToolViewRecordWithState:RecordVoiceStateBegin];
    }
}
// 发送录音
- (void)recordButtonDidFinish:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ESKeyBoardToolViewRecordWithState:)]) {
        [self.delegate ESKeyBoardToolViewRecordWithState:RecordVoiceStateFinish];
    }
}
// 取消
- (void)recordButtonDidCancle:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ESKeyBoardToolViewRecordWithState:)]) {
        [self.delegate ESKeyBoardToolViewRecordWithState:RecordVoiceStateCancle];
    }
}
二.语音录制/播放工具类 VoiceManager.h
//
//  VoiceManager.h
//  ZPS
//
//  Created by 张海军 on 2017/12/4.
//  Copyright © 2017年 baoqianli. All rights reserved.
//

#import 

@interface VoiceManager : NSObject
/// 当前录音url地址
@property (nonatomic, strong) NSURL *currentRecordUrl;
+ (instancetype)voiceManagerShare;
// 开始录制
- (void)beginRecordWithURL:(NSURL *)url;
// 停止/完成录制
- (void)stopRecordCompletion:(void(^)(BOOL finished,float duration))completion;
// 取消录制
- (void)cancleRecord;
// 播放
- (void)playAudioWithURL:(NSURL *)url;
@end
VoiceManager.m 包含录制时音量大小的动画 (在keyWindow上加一个view)
// 开始录音
- (void)beginRecordWithURL:(NSURL *)url{
    self.currentRecordUrl = url;
    [self getAudioRecorderWithUrl:url];
    [self.audioRecorder record];
    [self volumeBgView];
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(volumeChange) userInfo:nil repeats:YES];
    }
}

// 停止 / 完成
- (void)stopRecordCompletion:(void (^)(BOOL finish,float duration))completion{
    self.stopCompletion = completion;
    [self.audioRecorder stop];
    [self recordStopHandle];
}

// 取消
- (void)cancleRecord{
    [self.audioRecorder stop];
    BOOL delete = [self.audioRecorder deleteRecording];
    self.volumeStateLabel.text = @"取消发送";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self recordStopHandle];
    });
    NSLog(@"delete = %zd",delete);
}

// 开始播放
- (void)playAudioWithURL:(NSURL *)url{
    if (url == nil) {
        return;
    }
    [self getAudioPlayerWithUrl:url];
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer play];
}


///  设置音频会话
-(void)setAudioSession{
    AVAudioSession *audioSession=[AVAudioSession sharedInstance];
    //设置为播放和录音状态,以便可以在录制完之后播放录音
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];
    NSError *audioError = nil;
    BOOL success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioError];
    if(!success){
        NSLog(@"error doing outputaudioportoverride - %@", [audioError localizedDescription]);
    }
}

/// 取得录音文件设置
-(NSDictionary *)getAudioSetting{
    NSMutableDictionary *dicM=[NSMutableDictionary dictionary];
    //设置录音格式
    [dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
    //设置录音采样率,8000是电话采样率,对于一般录音已经够了
    [dicM setObject:@(44100) forKey:AVSampleRateKey];
    //设置通道,这里采用单声道
    [dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
    //每个采样点位数,分为8、16、24、32
    [dicM setObject:@(16) forKey:AVLinearPCMBitDepthKey];
    //是否使用浮点数采样
    [dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
    //....其他设置等
    return dicM;
}
三.点击开始录音后的回调处理
#pragma mark - ESKeyBoardToolViewDelegate
// 语音相关
- (void)ESKeyBoardToolViewRecordWithState:(RecordVoiceState)state{
    switch (state) {
        case RecordVoiceStateBegin:{
            NSString *fileName = [[NSString stringWithFormat:@"%zd",[[NSDate date] timeIntervalSinceReferenceDate]] stringByAppendingString:@".caf"];
            NSString *savePath = [[SocketManager shareSockManager].dataSavePath stringByAppendingPathComponent:[fileName lastPathComponent]];
            [[VoiceManager voiceManagerShare] beginRecordWithURL:[NSURL fileURLWithPath:savePath]];
        }
            break;
        case RecordVoiceStateFinish:{
            WS(weakSelf);
            [[VoiceManager voiceManagerShare] stopRecordCompletion:^(BOOL finished,float duration) {
                if (duration < 1.0) {
                    NSLog(@"时长小于1s 不发送");
                    return ;
                }
                ChatMessageModel *messageM = [ChatMessageModel new];
                messageM.isFormMe = YES;
                messageM.userName = [UIDevice currentDevice].name;
                messageM.chatMessageType = ChatMessageAudio;
                messageM.mediaMessageUrl = [VoiceManager voiceManagerShare].currentRecordUrl;
                messageM.mediaDuration = duration;
                messageM.fileName = [[NSString stringWithFormat:@"%zd",[[NSDate date] timeIntervalSinceReferenceDate]] stringByAppendingString:@".caf"];
                NSData *audioData = [NSData dataWithContentsOfURL:messageM.mediaMessageUrl options:NSDataReadingMappedIfSafe error:nil];
                messageM.fileSize = audioData.length;
                [weakSelf sendMessageWithItem:messageM];
            }];
        }
            break;
        case RecordVoiceStateCancle:{
            [[VoiceManager voiceManagerShare] cancleRecord];
        }
            break;
            
        default:
            break;
    }
}
四.SocketManager 类中对语音不需要进行单独的处理 跟视频/图片的传输方式是一样的
/// 发送数据
- (void)sendMessageWithItem:(ChatMessageModel *)item{
    item.atSendArrayIndex = self.needSendMoreItems.count;
    [self.needSendMoreItems addObject:item];
    if (self.needSendMoreItems.count < 2) {
        [self sendOneMessageItem:item];
    }else{
        
    }
}

- (void)sendOneMessageItem:(ChatMessageModel *)item{
    self.currentSendItem = item;
    NSData *textData = [self creationMessageDataWithItem:item];
    [self writeMediaMessageWithData:textData];
}

// 创建消息体
- (NSData *)creationMessageDataWithItem:(ChatMessageModel *)item{
    NSMutableDictionary *messageData = [NSMutableDictionary dictionary];
    messageData[@"fileName"] = item.fileName;
    messageData[@"userName"] = item.userName;
    messageData[@"chatMessageType"] = [NSNumber numberWithInt:item.chatMessageType];
    messageData[@"fileSize"] = [NSNumber numberWithInteger:item.fileSize];
    messageData[@"mediaDuration"] = [NSNumber numberWithFloat:item.mediaDuration];
    if (item.chatMessageType == ChatMessageText) {
        messageData[@"messageContent"] = item.messageContent;
    }else if (item.chatMessageType == ChatMessageImage || item.chatMessageType == ChatMessageVideo || item.chatMessageType == ChatMessageAudio){
        item.isWaitAcceptFile = YES;
        messageData[@"isWaitAcceptFile"] = [NSNumber numberWithBool:YES];
    }
    NSString *bodStr = [NSString hj_dicToJsonStr:messageData];
    return [bodStr dataUsingEncoding:NSUTF8StringEncoding];
}

// 图片或者视频文件传输
- (void)imageOrVideoFileSend:(ChatMessageModel *)sendItem{
    if (sendItem.chatMessageType == ChatMessageImage) {
        NSData *sendData = UIImagePNGRepresentation(sendItem.temImage);
        [self writeMediaMessageWithData:sendData];
    }else if (sendItem.chatMessageType == ChatMessageVideo){
        PHAsset *asset = (PHAsset *)sendItem.asset;
        [ZPPublicMethod getfilePath:asset Complete:^(NSURL *fileUrl) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                sendItem.mediaMessageUrl = fileUrl;
                NSData *sendData = [NSData dataWithContentsOfURL:sendItem.mediaMessageUrl options:NSDataReadingMappedIfSafe error:nil];
                [self writeMediaMessageWithData:sendData];
            });
        }];
    }else if (sendItem.chatMessageType == ChatMessageAudio){
        NSData *sendData = [NSData dataWithContentsOfURL:sendItem.mediaMessageUrl options:NSDataReadingMappedIfSafe error:nil];
        [self writeMediaMessageWithData:sendData];
    }
    
}

// 传输数据到服务端
- (void)writeMediaMessageWithData:(NSData *)sendData{
    self.currentSendTag += 1;
    self.currentSendItem.sendTag = self.currentSendTag;
    if (self.clientSocketArray.count > 0) {
        GCDAsyncSocket *clientSocket = [self.clientSocketArray firstObject];
        [clientSocket writeData:sendData withTimeout:-1 tag:self.currentSendItem.sendTag];
    }else{
        [self.tcpSocketManager writeData:sendData withTimeout:-1 tag:self.currentSendItem.sendTag];
    }
}

// 媒体文件接受完成后发送的消息
- (void)sendMediaAcceptEndMessage{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSData *data = [FILE_ACCEPT_END dataUsingEncoding:NSUTF8StringEncoding];
        self.currentSendItem = nil;
        if (self.clientSocketArray.count > 0) {
            GCDAsyncSocket *clientSocket = [self.clientSocketArray firstObject];
            [clientSocket writeData:data withTimeout:-1 tag:-99999];
        }else{
            [self.tcpSocketManager writeData:data withTimeout:-1 tag:-99999];
        }
    });
}

// 发送下一个消息体
- (void)sendNextMessage{
    if (self.needSendMoreItems.count > 0) {
        [self sendOneMessageItem:[self.needSendMoreItems firstObject]];
    }
}

你可能感兴趣的:(局域网内端到端的聊天项目(七))