<scriptsetuplang="ts">import{ onMounted, reactive, ref }from'vue';import useInject from'@/utils/useInject';constprops: any = defineProps<{params?: any;}>();const{ $global, $fn }=useInject();constwebmObj: any =reactive({stat:0,blob:null,dataChunks:[],newRecorder:null,});conststartBtn: any =ref(null);conststopBtn: any =ref(null);constdownBtn: any =ref(null);constpreVideo: any =ref(null);constrecVideo: any =ref(null);onMounted(()=>{navigator.permissions.query({name:'camera'}).then((res)=>{if(res.state ==='denied'){// $global.tipsfn({ type: 'err', con: '已拒绝授权录像' });}});// 开始录制functionstartRecording(stream: any,lengthInMS: any =null){webmObj.newRecorder =newMediaRecorder(stream);webmObj.newRecorder.ondataavailable=(event: any)=>{let data = event.data;webmObj.dataChunks.push(data);};webmObj.newRecorder.start(1000);console.log(webmObj.newRecorder.state +' start to recording .....');}stopBtn.value.addEventListener('pointerdown',()=>{webmObj.stat =0;// close the recordingrecVideo.value.srcObject.getTracks().forEach((track: any)=> track.stop());webmObj.newRecorder.stop();// Play recorded videowebmObj.blob =newBlob(webmObj.dataChunks,{type:'video/webm'});console.log(666.30003,'视频数据Blob',webmObj.dataChunks,webmObj.blob);preVideo.value.src =URL.createObjectURL(webmObj.blob);// Save download video, click the download button, you can download itdownBtn.value.href = preVideo.value.src;downBtn.value.download ='RecordedVideo.webm';});startBtn.value.addEventListener('pointerdown',()=>{webmObj.stat =1;// get the streamnavigator.mediaDevices.getUserMedia({audio:true,video:true,}).then((stream)=>{// set the stream to left videorecVideo.value.srcObject = stream;// set the stream to <a> for downloaddownBtn.value.href = stream;// captureStream: which is streaming a real-time capture of the content being rendered in the media element.// A MediaStream object which can be used as a source for audio or video data by other mediarecVideo.value.captureStream =recVideo.value.captureStream ||recVideo.value.mozCaptureStream;startRecording(recVideo.value.captureStream());}).catch((err)=>{console.log('recording error: ', err);});});});functionupvideo(){if(webmObj.blob){var reader =newFileReader();reader.readAsDataURL(webmObj.blob);reader.onload=function(){$fn.useApiFiles().request({data:{dir:'chat/chat',uid: $fn.As.Uuid('CV'),ext:'.webm',data: reader.result,},}).then((res: any)=>{console.log(666.789, res);props?.params?.fn(res);}).catch((err: any)=>{$global.tipsfn({type:'err',con: err });});};}}</script><template><divclass="as-webm-area"><divclass="as-webm-show-area"><videov-show="!webmObj.stat"ref="preVideo"controls></video><videov-show="webmObj.stat"ref="recVideo"autoplaymuted></video></div><divclass="as-btn-area"><buttonref="startBtn">开始录制</button><buttonref="stopBtn">停止录制</button><buttonref="downBtn">下载</button><button@click="upvideo">提交保存</button></div></div></template><stylescoped>.as-webm-area{width: 100%;height: 100%;display: flex;flex-direction: column;}.as-webm-show-area{flex-grow: 1;width: 100%;height: 100%;overflow: hidden;}.as-webm-show-area > video{width: 100%;height: 100%;}.as-btn-area{text-align: center;height: auto;}.as-btn-area > button{user-select: none;padding: 5px 12px;margin: 15px 5px 0 5px;}</style>