您的位置:首页 > 其它

apicloud——融云聊天发送语音消息

2018-02-04 14:19 148 查看
apicloud官方封装了融云即时通讯的聊天软件,做手机聊天模块可以直接引用apicloud的官方模块,引用模块首先需要在app内配置一下,配置的内容要在融云官网注册开发者账号,然后创建app,获得appKey,并在config文件中进行配置。

下面是配置文件:



下面是事例代码:

/* 窗口请求模块初始化 */
window_fun.init = function (){
//初始化UIChatBox模块
UIChatBox = api.require('UIChatBox'); //请求UIChatBox模块
//初始化融云模块
rong = api.require('rongCloud2'); //请求融云模块
if(api.pageParam.uid){
window_fun.target_userId = api.pageParam.uid;
console.log(window_fun.target_userId);
}
window_fun.user = $api.getStorage('userData');
console.log($api.jsonToStr(window_fun.user));
}

/* 融云模块初始化 */
rongCloud2.rong_init = function(){
//初始化融云模块
rong.init(function(ret, err) {
if (ret.status == 'error')
api.toast({ msg: err.code });
});
}

/* 融云模块连接,监听连接状态 */
rongCloud2.rong_connect = function(user_token){
console.log('连接监听');
//监听融云的连接状态
rong.setConnectionStatusListener(function(ret, err) {
api.toast({ msg: ret.result.connectionStatus });
});
var user_token = window_fun.user.userToken;
console.log(user_token);
//连接到融云 token:1tnI2N8cHDmT+Uyc2sub36Iz5iPSavlq7XaXGVCWCrmN1QwgruKwjxi3hxZA02Il8AgUlO5k6HxOlrDEFLWk5w==$api.getStorage('userToken')
rong.connect({
token: window_fun.user.userToken}, //用户的token
function(ret, err) { //回调函数
console.log('ret.result.userId');
if (ret.status == 'success'){ //成功
api.toast({ msg: ret.result.userId });
$api.setStorage('rong_uid',ret.result.userId); //存储id到web存储中
console.log('web存储'+$api.getStorage('rong_uid'));
console.log('连接成功'+ret.result.userId);
}else { //失败
api.toast({ msg: err.code+'连接失败'});
console.log(err.code+'连接失败');
}
});
}

/* 融云模块发送语音消息,参数obj_redio:语音消息数据对象(obj_redio.voicePath:语音路径,obj_redio.duration:语音时长) */
rongCloud2.rong_sendVoice = function(obj_redio){
//融云模块发送语音方法
rong.sendVoiceMessage({
conversationType: 'PRIVATE', //会话类型,参数PRIVATE:单聊
targetId: window_fun.target_userId, //目标用户id
voicePath: obj_redio.voicePath, //音频路径
duration: obj_redio.duration, //音频时长
extra: $api.jsonToStr(window_fun.user) //额外信息
}, function(ret, err) { //回调函数,ret:返回信息,err:错误码
if (ret.status == 'prepare'){ //发送状态,准备
api.toast({ msg: JSON.stringify(ret.result.message) });
}else if (ret.status == 'success'){ //发送状态,成功
api.toast({ msg: ret.result.message.messageId });
console.log("成功");
chat_view.sendRedio(window_fun.user.userImage, obj_redio); //将发送的语音信息铺写到聊天界面
}else if (ret.status == 'error'){ //发送状态,失败
api.toast({ msg: err.code });
}
});
}

/* 发送语音信息,参数headSrc:头像路径,参数obj_redio:语音信息数据对象(obj_redio.voicePath:语音路径,obj_redio.duration:语音时长) */
chat_view.sendRedio = function(headSrc, obj_redio){
var path = "'"+obj_redio.voicePath+"'"; //定义一个路径变量
var video_box_width = parseInt(obj_redio.duration); //定义气泡的宽度
if(video_box_width == 1){ //如果是1s宽度为50px
video_box_width = 50;
}else if(video_box_width <= 20){ //如果是小于等于20秒气泡的宽度度为时长乘上10加上50px
video_box_width = (video_box_width*10+50);
}else { //最大气泡宽度为250px
video_box_width = 250;
}
/* 气泡加上数据的html代码 */
var html = '<div class="show">'+
'<div class="msg" >'+
'<img src="'+ headSrc +'" style="width:35px;height:35px"/>'+
'<p style="background-color:#9eea6a;color:#000000;width:'+video_box_width+'px;padding-left:8px" onClick="chat_view.playVedio('
+path+')">'+
'<i class="msg_input"></i>'+ obj_redio.duration +
'\'</p >'+
'</div>'+
'</div>';
chat_view.upView(html);//刷新视图
}
/* UIChatBox录制和发送语音 */
UIChatMethod.UIChat_sendVoice = function(){
 //监听按钮
 UIChatBox.addEventListener({                                                   //按压录音按钮监听
     target: 'recordBtn',                                                       //按钮名称
     name: 'press'                                                              //按钮事件
 }, function(ret, err) {
     if (ret) {                                                                 //按压成功
        //开始录音
          toast.custom({
              title:"正在录音",
              html:'<i class="aui-iconfont aui-icon-comment"></i>',
              duration:60000
          });
         api.startRecord({
         });
     } else {
         alert(JSON.stringify(err));
     }
 });

 //监听按钮
 UIChatBox.addEventListener({                                                   //松开录音按钮监听
         target: 'recordBtn',                                                   //按钮名称
         name: 'press_cancel'                                                   //按钮事件
     }, function(ret, err) {
         if (ret) {                                                             //松开成功
             toast.hide();                                                      //隐藏toast
             //停止录音
             api.stopRecord(function(ret, err) {
               var obj_redio;
                 if (ret) {//如果录音成功发送语音信息
                     console.log(ret.path+"11111"+ret.duration);
                     //定义一个语音对象,存放语音的路径和语音的时长
                     var obj_redio = {
                       "voicePath": ret.path,                                   //语音的路径
                       "duration": ret.duration,                                //语音的时长
                     }
                     //融云发送语音
                     rongCloud2.rong_sendVoice(obj_redio);
                 }
             });
         } else {
             alert(JSON.stringify(err));
         }
     });
}

/* UIChatBox取消发送语音 */
UIChatMethod.UIChat_cancelSendVoice = function(){
  var UIChatBox = api.require('UIChatBox');
  UIChatBox.addEventListener({
      target: 'recordBtn',
      name: 'move_out'
  }, function(ret, err) {
      if (ret) {
        toast.hide();                                                           //隐藏toast
        //停止录音
        api.stopRecord(function(ret, err) {
            if (ret) {                                                          //如果录音成功发送语音信息
                api.toast({msg:"取消发送"});
            }
        });
      } else {
      }
  });
}


发送语音的时候要确认app开启了手机的录制语音的权限,引用模块后要是用自定义apploader进行编译调试。在这里我引用了两个模块,rongcloud2和UIChatbox。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: