pages-chat-speechtotext.96acf07c.js 2.9 KB

1
  1. import{s as e,K as a,Y as s,ae as t,e as o,f as l,w as n,l as u,a2 as c,A as r,k as i,r as v,F as d,j as p,t as m,u as h,g as f,G as g,m as y,x as w}from"./index-a9fee078.js";import{_}from"./s-layout.85d4d27b.js";import{_ as b}from"./_plugin-vue_export-helper.1b428a4d.js";import"./form.6bc3e315.js";const x=""+new URL("audioPaly-372169db.png",import.meta.url).href,M=b({__name:"speechtotext",setup(b){const{safeAreaInsets:M,safeArea:j}=e.$platform.device,D=e.$platform.navbar,I=j.height-D-20-60,T=a([]),U=a(!1);let k=a(null);const A=a("");let E=null;const R=a(0);let L=null,P=[],Y=a(!1);s((()=>{navigator.mediaDevices.getUserMedia({audio:!0}).then((e=>{L=new MediaRecorder(e),L.ondataavailable=e=>{P.push(e.data)},L.onstop=()=>{Y.value||z(),P=[]}})).catch((e=>{console.error("Error accessing media devices.",e)}))}));const z=()=>{const e=new Blob(P,{type:"audio/mpeg"}),a=URL.createObjectURL(e),s=Math.max(Math.floor((new Date-k.value)/1e3),1);T.value.push({duration:s,audioUrl:a}),async function(e){const a=new FormData;a.append("audio_file",e);try{const e=await fetch("https://zxgz.newfeifan.cn/voice2text/",{method:"POST",body:a}),s=await e.json();console.log("Server response:",s)}catch(s){console.error("Error sending audio file:",s)}}(e),t((()=>{let e=document.getElementById("messages");e.scrollTop=e.scrollHeight}))},B=()=>{const e=Math.floor((new Date-k.value)/1e3);A.value=e+"s",e>=60&&F(new Event("mouseup"))},C=e=>{U.value||"mousedown"!==e.type&&"touchstart"!==e.type||(k.value=new Date,U.value=!0,E=setInterval(B,1e3),A.value="1s",e.preventDefault(),R.value=e.touches?e.touches[0].clientY:0,P=[],Y.value=!1,L.start())},F=e=>{!U.value||"mouseup"!==e.type&&"touchend"!==e.type&&"mouseleave"!==e.type&&"touchcancel"!==e.type||(clearInterval(E),U.value=!1,A.value="",new Date-k.value>=500?L.stop():console.log("录音时间太短,不保存"))},O=()=>{U.value&&(clearInterval(E),console.log("录音取消"),Y.value=!0,L.stop(),S())},S=()=>{U.value=!1,A.value=0,k.value=null,P=[]},$=e=>{const a=e.touches[0].clientY;R.value-a>280&&O()};return(e,a)=>{const s=w,t=g,b=u(y("s-layout"),_);return o(),l(b,{class:"chat-wrap",title:"语音转文字"},{default:n((()=>[c("div",{class:"chat-container"},[c("div",{class:"messages",style:r({height:I+"px"}),id:"messages"},[(o(!0),i(d,null,v(T.value,((e,a)=>(o(),i("div",{key:a,class:"message",onClick:e=>(e=>{const a=T.value[e];new Audio(a.audioUrl).play()})(a)},[c("div",{class:"bubble"},[c("span",{class:"duration"},h(e.duration)+'" ',1),c("span",{class:"ss-m-l-10"},[p(s,{src:x,class:"audioPaly"})])])],8,["onClick"])))),128))],4),c("div",{class:"input-area"},[p(t,{onMousedown:C,onMouseup:F,onMouseleave:O,onTouchstart:C,onTouchend:F,onTouchcancel:O,onTouchmove:$},{default:n((()=>[m(" 按住 说话 ")])),_:1}),U.value?(o(),i("div",{key:0,class:"recording-overlay"},[c("div",null,h(A.value),1),c("div",null,"上滑至此取消")])):f("",!0)])])])),_:1})}}},[["__scopeId","data-v-b7d325af"]]);export{M as default};