首页 > 其他分享 >在线客服系统访客发送录音功能,在elementui中使用js-audio-recorder实现录音功能,然后上传文件发送出去

在线客服系统访客发送录音功能,在elementui中使用js-audio-recorder实现录音功能,然后上传文件发送出去

时间:2023-04-18 23:45:10浏览次数:33  
标签:function elementui percentage 录音 发送 error recorder message

访客在聊天界面中可以发送语音,其实就是录音以后,调用上传接口,把录音文件发送给客服。

 点击麦克图标以后,展示出一个elementui的dialog弹窗,里面展示四个功能按钮。

分别是,开始录音,结束录音,取消录音,发送录音。基本流程就是点开始,然后点结束,再点发送。

下面是聊天界面中的dialog弹窗 ,另外我还增加了一个进度条的展示,超过60秒就结束录音,以及展示录音文件

        <!--录音-->
        <el-dialog
                :visible.sync="audioDialog"
                width="100%"
                center
        >
            <div class="dialogRecoder">
                <el-progress :color="colors" type="dashboard" :format="recoderFormat" :stroke-width="10" :percentage="recoderSecond"></el-progress>
                <br/>
                <audio v-show="recorderEnd!=null" controls ref="audio" muted="muted" src="" id="audio"></audio>
                <br/>
                <el-button @click="startRecoder()" size="small" type="primary">開始</el-button>
                <el-button @click="stopRecoder()" size="small" type="warning">結束</el-button>
                <el-button @click="cancelRecoder()" size="small" type="danger">取消</el-button>
                <el-button @click="sendRecoder()" size="small" type="success">發送</el-button>
            </div>
        </el-dialog>
        <!--//录音-->

 

另外,我的代码是根据我自己的项目情况,直接摘抄出来的,请结合自己项目进行修改

然后安装js-audio-recorder  

npm i js-audio-recorder

使用方式是  import Recorder from 'js-audio-recorder'

然后就是那四个操作方法了,其中的data属性是

              //录音
              recorder:null,
              audioDialog:false,
              recoderSecond:0,
              recorderEnd:null,
              colors: [
                  {color: '#f56c6c', percentage: 20},
                  {color: '#e6a23c', percentage: 40},
                  {color: '#5cb87a', percentage: 60},
                  {color: '#1989fa', percentage: 80},
                  {color: '#6f7ad3', percentage: 100}
              ],

method部分是

             //开始录音
            startRecoder:function(){
                if(this.recorder){
                    this.recorder.destroy();
                    this.recorder=null;
                }
                var _this=this;
                Recorder.getPermission().then(function() {
                    _this.recorder = new Recorder();
                    _this.recorderAudio = document.querySelector('#audio');
                    _this.recorder.start();
                    _this.recorder.onprogress = function (params) {
                        _this.recoderSecond = parseInt(params.duration);
                        if(_this.recoderSecond>=60) _this.stopRecoder();
                    }
                }, function(error){
                    _this.$message({
                        message: error,
                        type: 'error'
                    });
                    return;
                });
            },
            //结束录音
            stopRecoder:function(){
                if(!this.recorder){
                    return;
                }
                var blob=this.recorder.getWAVBlob();
                this.recorderAudio.src = URL.createObjectURL(blob);
                this.recorderAudio.controls = true;
                this.recorderEnd=blob;
                this.recorder.destroy();
                this.recorder=null;
            },
            //发送录音
            sendRecoder:function(){
                this.stopRecoder();
                if(!this.recorderEnd) return;
                
                let _this=this;
                let formData = new FormData();
                formData.append("realfile", this.recorderEnd); //传给后台的file的key值是可以自己定义的
                fetch(_this.ApiHost+'/2/uploadAudio', {
                  method: "POST",
                  body: formData
                })
                .then(response => response.json())
                .then(res => {
                  console.log(res);
                    if(res.code!=200){
                      _this.$message({
                          message: res.msg,
                          type: 'error'
                      });
                    }else{
                      _this.$message({
                          message: "success!",
                          type: 'success'
                      });
                      _this.cancelRecoder();
                      _this.visitor.message='audio[' + res.result.path+ ']';
                      _this.chatToUser();
                    }
                })
                .catch(error => {
                  console.error(error);
                });
            },
            //取消录音
            cancelRecoder:function(){
                this.audioDialog=false;
                if(!this.recorder){
                    return;
                }
                this.recorder.destroy();
                this.recorder=null;
                this.recoderSecond=0;
            },
            //录音的百分比
            recoderFormat:function(percentage){
                return percentage+"s";
            },

 

标签:function,elementui,percentage,录音,发送,error,recorder,message
From: https://www.cnblogs.com/taoshihan/p/17331509.html

相关文章

  • https发送post请求报错403,但不是跨域问题。
    这段时间在公司写一个项目,在项目中遇到一个问题,查阅了网上很多资料都没解决。问题是vue中https发送post请求报错403,但不是跨域问题。 于是我根据网上资料发现是请求头部的问题。源代码 headers:{  'Content-Type':'application/x-www-form-urlencoded', },当......
  • rsyslog读取应用服务器nginx日志文件并发送至日志服务器
    现将云主机上的nginx服务的日志发送到日志服务器进行归档备份,后期还会考虑对备份后的nginx日志进行ELK分析,目前因为只是简单的备份日志文件,所以我就使用rsyslog来完成日志的备份。目标:使用rsyslog服务同步nginx服务的access.log和error.log日志文件到日志服务器。说明:一台部署......
  • 在线客服系统聊天界面,增加emoji表情图标,纯emoji点击后发送
    之前开发客服系统访客聊天界面,发送表情部分,是自己本地的一堆小图片现在其实可以直接展示emoji,效果也是不错的,还不需要自己再去解析表情路径 首先准备一个emoji的json字符串emojis:{"smile":"......
  • elementUI Table 表格编辑数据后停留当前位置
    后台管理系统在实际开发中,表格如果在一定高度出现滚动条。这时如果对表格行数据进行编辑或者拖拽排序操作,数据刷新后滚动条会默认回到顶部,这样体验会不太好。如果想保留在当前位置可以这样操作:1.el-table标签添加ref属性<el-table:data="tableData"v-loading="tableLoading......
  • elementui select下来内容过长问题解决方案
    :popper-append-to-body="false"必写自定义显示<divclass="select-flow">{{dict.declareConditions}}</div>自定义css样式el-option添加title属性 <el-selectv-model="formData.declCondition"placeholder="请选择"sty......
  • Springboot使用RestTemplate发送Post请求postForEntity (application/json)的坑
    当使用RestTemplate进行http请求时,的确很方便,但是当需要进行post请求时遇到了坑1POST传递参数:采用LinkedMultiValueMap,不能使用HashMapStringurl='http://posturl';MultiValueMap<String,String>map=newLinkedMultiValueMap<String,String>();map.add(......
  • elementui表单循环添加校验
    1<el-form2:model="dynamicValidateForm"3ref="dynamicValidateForm"4:inline="true"5>6<templatev-for="(item,index)indynamicValidateForm.domains">......
  • thinkphp:redis+lua实现短信发送频率限制(thinkphp v6.0.12LTS)
    一,配置:.env中[REDIS0]TYPE=redisHOST=127.0.0.1PORT=6379PASSWORD=二,php代码:1,lib\util\SmsRateUtil.php<?phpnamespaceapp\lib\util;//短信验证码发送频率classSmsRateUtil{//redis连接private$redis;//60秒内不允许重复发送pri......
  • epic你似乎发送了两次请求解决方法
    epic的知名度在游戏界很高,尤其是他会时不时提供免费游戏收到很多玩家追捧,但是很多朋友都遇到了你似乎发送了两次请求的问题,下面就给各位带来详细的解决方法。【epicgame游戏平台详细介绍】【付款正在处理中】epic你似乎发送了两次请求怎么解决:1、如果你是wim10的系统,最简单的......
  • uni-app中IM的发送的修改以及回滚底部的修改
    在使用的过程中发现输入文字之后没有相关的发送按钮,所以对TUIKit/TUIPages/TUIChat/components/message-input/index.vue做微调,如下微调内容:添加@input方法<inputclass="TUI-message-input-area":adjust-position="true"cursor-spacing="20"......