NO1.首先前往融云官网申请应用apk
NO2.下载SDK
大家注意一下融云有两种SDK
我们下载第二种就可以了,包比较小,节约空间
接着我们把包拖到工程
然后我们添加相应的库
您除了在工程中导入 SDK 之前,还需要添加如下系统库的引用。
AssetsLibrary.framework
AudioToolbox.framework
AVFoundation.framework
CFNetwork.framework
CoreAudio.framework
CoreGraphics.framework
CoreLocation.framework
CoreMedia.framework
CoreTelephony.framework
CoreVideo.framework
ImageIO.framework
libc++.tbd
libc++abi.tbd
libsqlite3.tbd
libstdc++.tbd
libxml2.tbd
libz.tbd
MapKit.framework
OpenGLES.framework
QuartzCore.framework
SystemConfiguration.framework
UIKit.framework
Photos.framework
SafariServices.framework
NO3.注册融云
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里面添加
[[RCIMClient sharedRCIMClient] initWithAppKey:zyAppKey];
[[RCIMClient sharedRCIMClient] connectWithToken:zyc_IM_Token
success:^(NSString *userId) {
NSLog(@"登陆成功。当前登录的用户ID:%@", userId);
} error:^(RCConnectErrorCode status) {
NSLog(@"登陆的错误码为:%d", status);
} tokenIncorrect:^{
//token过期或者不正确。
//如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
//如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
NSLog(@"token错误");
}];
appkey zyc_IM_Token就是上面我们获取的key 和 Token
NO4.实现录音
导入 AVFoundation/AVFoundation.h
我们用系统的AVAudioSession来实现录音,官方建议使用wav格式
-(void)zyplay{
NSLog(@"开始录音");
AVAudioSession *session =[AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if (session == nil) {
NSLog(@"Error creating session: %@",[sessionError description]);
}else{
[session setActive:YES error:nil];
}
self.session = session;
//1.获取沙盒地址
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
filePath = [path stringByAppendingString:@"/RRecord.wav"];
//2.获取文件路径
self.recordFileUrl = [NSURL fileURLWithPath:filePath];
//设置参数
NSDictionary *settings = @{AVFormatIDKey: @(kAudioFormatLinearPCM),
AVSampleRateKey: @8000.00f,
AVNumberOfChannelsKey: @1,
AVLinearPCMBitDepthKey: @16,
AVLinearPCMIsNonInterleaved: @NO,
AVLinearPCMIsFloatKey: @NO,
AVLinearPCMIsBigEndianKey: @NO};
_recorder = [[AVAudioRecorder alloc] initWithURL:self.recordFileUrl settings:settings error:nil];
if (_recorder) {
_recorder.meteringEnabled = YES;
[_recorder prepareToRecord];
[_recorder record];
}else{
NSLog(@"音频格式和文件存储格式不匹配,无法初始化Recorder");
}
}
这样我们就录制了RRecord.wav这个语音
NO4.发送语音
我们先转换上面我们的wav数据
NSData *kk = [NSData dataWithContentsOfURL:self.recordFileUrl];
self.recordFileUrl是上面RRecord.wav的地址
- (void)sendVoiceMessage:(NSData *)wavData duration:(long)duration targetId:(NSString *)targetId conversationType:(RCConversationType)conversationType {
//语音消息实体
RCVoiceMessage *voiceMessage = [RCVoiceMessage messageWithAudio:wavData duration:duration];
// [self.player play];
[[RCIMClient sharedRCIMClient] sendMessage:conversationType targetId:targetId content:voiceMessage pushContent:nil pushData:nil success:^(long messageId) {
NSLog(@"发送语音消息成功");
} error:^(RCErrorCode nErrorCode, long messageId) {
NSLog(@"发送语音消息失败,错误码是(%ld)", (long)nErrorCode);
}];
}
RCVoiceMessage是语音消息体 wavData是Data化的wav
NO5.接收语音
我们先实现代理
RCIMClientReceiveMessageDelegate 代理
[[RCIMClient sharedRCIMClient] setReceiveMessageDelegate:self object:nil];
- (void)onReceived:(RCMessage *)message
left:(int)nLeft
object:(id)object {
if ([message.content isMemberOfClass:[RCVoiceMessage class]]) {
NSLog(@"收到语音消息");
RCVoiceMessage *voiceMsg = (RCVoiceMessage *)message.content;
self.wavAudioData = voiceMsg.wavAudioData;
[self voicePressed];
}
NSLog(@"还剩余的未接收的消息数:%d", nLeft);
}
- (void)setWavAudioData:(NSData *)wavAudioData {
_wavAudioData = wavAudioData;
NSError *error;
AVAudioPlayer *player =
[[AVAudioPlayer alloc] initWithData:wavAudioData error:&error];
self.player = player;
self.player.delegate = self;
}
NO6.播放语音
- (void)voicePressed {
NSLog(@"pressed");
if (self.isPlaying) {
[self stop];
} else {
[self playing];
}
}
- (void)playing {
self.isPlaying = YES;
[self.player prepareToPlay];
[self.player play];
}
- (void)stop {
self.isPlaying = NO;
[self.player stop];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag {
self.isPlaying = NO;
}
NO7.我们加入一个聊天室来测试一哈
- (void)zyaddID:(NSString *)ID{
//
[[RCIMClient sharedRCIMClient] joinChatRoom:ID messageCount:1 success:^{
NSLog(@"加入聊天室");
} error:^(RCErrorCode status) {
NSLog(@"加入聊天室失败");
}];
[[RCIMClient sharedRCIMClient] setReceiveMessageDelegate:self object:nil];
}
-(void)zyquitID:(NSString *)ID{
[[RCIMClient sharedRCIMClient] quitChatRoom:ID success:^{
NSLog(@"退出聊天室");
} error:^(RCErrorCode status) {
NSLog(@"退出聊天室失败");
}];
}