项目中的pc录音功能是首创在一个事件中进行录音,实现录音功能。我直接上传代码//自己配音:manualDubbingold:function(type){letthat=this;navigator.mediaDevices.getUserMedia({audio:true}).then(stream=>{console.log("Arecordingwascreated");constrecordedChunks=[];varmediaRecorder=newMediaRecorder(stream);//创建记录器instancealert//lettime=(that.subtitle[index].end_time-that.subtitle[index].begin_time)*1000//录音总时长that.self_voice.time=(time/1000).toFixed(1)-0;if(type==="start"){lets_timer=null;lettimer_djs=setInterval(()=>{//3210倒计时that.self_voice.num--;if(that.self_voice.num===0){clearInterval(timer_djs);setTimeout(()=>{mediaRecorder.start();//开始录音},200);that.self_voice.num=3;that.self_voice.is_show_num=false;that.self_voice.is_recording=true;//倒计时:s_timer=setInterval(()=>{that.self_voice.time=(that.self_voice.time*10-0.1*10)/10;//if(that.self_voice.time==0){//clearInterval(s_timer)//}},100);//当时间到了,录音会自动停止://letrecord_timer=setTimeout(()=>{//mediaRecorder.stop()//stop//clearTimeout(record_timer)//},time)}},1000);//监听开始记录:}if(type==="stop"){clearInterval(s_timer);//停止定时器mediaRecorder.stop();//停止}mediaRecorder.addEventListener("start",e=>{//console.log('Startingrecording')});媒体记录器.addEventListener("dataavailable",e=>{if(e.data.size>0)recordedChunks.push(e.data);//console.log('recording')});//监听结束录音:mediaRecorder.addEventListener("stop",e=>{letdataWithType=newBlob(recordedChunks,{type:"audio/x-wav"});that.self_voice.file=dataWithType;//获取theaudiothat.self_voice.is_recording=false;that.self_voice.submit_active=true;that.self_voice.src=URL.createObjectURL(newBlob(recordedChunks));//Preview//录音后添加录音if(!that.self_voice.submit_active)return;clearOrAddVoice(that,that.dubbingindex,that.self_voice.file);});}).catch(error=>{that.$message({showClose:true,type:"error",message:"NotfoundAudiodevicemicrophone",duration:3000});});},这样就可以实现定时记录时间完成##后自动结束录制第二种是通过文件中的其他事件停止录制,即手动暂停(我用的是插件)。csdn的大佬文章很有用。这;if(type==="start"){that.stopbtn=false;//点击暂停使暂停重置变为双竖线that.captureErcord();//记录方式}else{if(!that.stopbtnchoose)return;this.mediaRecorder.stop();clearInterval(this.intervaltimerid);this.mediaRecorder.stream.stop();}},captureErcord:function(){//记录方法console.log("1点击记录",newDate());navigator.mediaDevices.getUserMedia(this.mediaConstraints).then(this.onMediaSuccess).catch(this.onMediaError);},//错误处理方法onMediaError:function(e){console.log("啊,你的浏览器好像不支持录音...",e);clearInterval(this.intervaltimerid);this.$message.error("您的浏览器不支持录音功能");},onMediaSuccess:function(stream){console.log("2次成功录制",newDate());让那个=这个;让timer_djs=设置Interval(()=>{//3210倒计时that.self_voice.num--;if(that.self_voice.num===0){clearInterval(timer_djs);that.self_voice.num=3;that.self_voice.is_show_num=false;that.self_voice.is_recording=true;that.stopbtnchoose=true;//让暂停鼠标上移改变that.mediaRecorder=newMediaStreamRecorder(stream);//获取音频流that.mediaRecorder.stream=stream;that.mediaRecorder.mimeType="audio/wav";that.self_voice.shiting_active=false;that.mediaRecorder.ondataavailable=function(blob){console.log("3录音",newDate());clearInterval(that.intervaltimerid);consturl=URL.createObjectURL(blob);//_that.$emit('handleStop',{//url:url,//mblob:blob//})that.self_voice.src=url;//预览//_that.self_voice.index=_that.dubbingindex//记录下标//_that.dubbingData[_that.dubbingindex]=url//预览列表};that.mediaRecorder.start(60*1000);//录音时间必须是//定义间隔that.intervaltimerid=setInterval(()=>{//开始累计//持续时间倒计时:that.self_voice.time=(that.self_voice.time*10-0.1*10)/10;if(that.self_voice.time<=0){//hengthat.timecolor=true;}},100);}},1000);},方法仅供参考,毕竟各个项目需求不同
