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

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

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

// 新增语音/语音录制按钮
@interface ESKeyBoardToolView () <UITextViewDelegate>
/// 语音按钮
@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 <Foundation/Foundation.h>

@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]];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,449评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,019评论 4 62
  • 姑娘 牵着手 走在烈日下 走在青石板上 走在斑驳的古城中 这是我从未有过的体验 我想弹起吉他为你唱首歌 一首只属于...
    吕院长阅读 263评论 2 3
  • 杨朱学派:两千年绝学及新诠释 【作 者】李伯聪 杨朱其人两千多年来蒙受恶名,其事迹无载于正史。但许多可靠的证据表明...
    gdlyz阅读 1,165评论 0 2
  • 【初心】 宝贝,因为出生时你呛到了羊水 所以你的出生让家人揪心...
    爱华王阅读 407评论 3 6