<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; media-src blob:"><style>
html,body{margin:0;height:100%;background:#F7F4EE;font:14px sans-serif;color:#2B211A}button{width:100%;height:100%;border:1px solid #EDE7DC;border-radius:10px;background:#F2EADA;color:#2B211A;font-size:14px;touch-action:none;user-select:none}button.recording{background:#D1AB69}button.cancel{background:#B9514D;color:white}
</style></head><body><button id="hold" aria-label="按住说话">按住说话</button><script>
const button=document.getElementById('hold'),post=x=>window.jdj?.postMessage(typeof x==='string'?x:JSON.stringify(x));
let pressed=false,cancelled=false,busy=false,recorder,stream,started=0,origin=0,epoch=0,timeout,timer;
function release(){clearTimeout(timeout);clearInterval(timer);stream?.getTracks().forEach(t=>t.stop());stream=null;button.className='';button.textContent='按住说话';}
window.setSending=value=>{busy=value;button.disabled=value;};
window.stopMedia=()=>{pressed=false;epoch++;if(recorder?.state==='recording'){recorder.onstop=null;recorder.stop();}release();};
window.permissionReady=async()=>{if(!pressed||busy||stream)return;const stamp=++epoch;try{const capture=await navigator.mediaDevices.getUserMedia({audio:true,video:false});if(!pressed||stamp!==epoch){capture.getTracks().forEach(t=>t.stop());return;}stream=capture;const mime='audio/webm;codecs=opus';if(!MediaRecorder.isTypeSupported(mime))throw Error('请更新 Android System WebView 后使用语音');const chunks=[];recorder=new MediaRecorder(stream,{mimeType:mime,audioBitsPerSecond:64000});
recorder.ondataavailable=e=>{if(e.data.size)chunks.push(e.data);};recorder.onstop=async()=>{const stamp=epoch,short=performance.now()-started<600;const discard=cancelled||short;release();if(discard){post({type:'status',message:short?'说话时间太短':'已取消语音'});return;}window.setSending(true);const blob=new Blob(chunks,{type:'audio/webm'}),bytes=new Uint8Array(await blob.arrayBuffer());if(stamp!==epoch){window.setSending(false);return;}post({type:'start'});for(let p=0;p<bytes.length;p+=90000){let s='';for(const b of bytes.subarray(p,p+90000))s+=String.fromCharCode(b);post({type:'chunk',data:btoa(s)});}post({type:'complete'});};
recorder.start(250);started=performance.now();button.className='recording';button.textContent='松开发送 · 上滑取消';post({type:'status',message:'正在录音,松开发送,上滑取消'});timer=setInterval(()=>{if(!cancelled)button.textContent=Math.ceil((performance.now()-started)/1000)+' 秒 · 松开发送';},500);timeout=setTimeout(()=>{pressed=false;recorder.stop();},59500);
}catch(e){release();post({type:'status',message:e.message});}};
button.onpointerdown=e=>{if(busy)return;e.preventDefault();button.setPointerCapture(e.pointerId);pressed=true;cancelled=false;origin=e.clientY;post({type:'permission'});};
button.onpointermove=e=>{if(!pressed)return;cancelled=origin-e.clientY>65;if(recorder?.state==='recording'){button.className=cancelled?'cancel':'recording';button.textContent=cancelled?'松开取消':'松开发送 · 上滑取消';}};
function end(e,cancel){e.preventDefault();pressed=false;if(cancel)cancelled=true;if(recorder?.state==='recording')recorder.stop();else{epoch++;release();}}
button.onpointerup=e=>end(e,false);button.onpointercancel=e=>end(e,true);window.addEventListener('pagehide',window.stopMedia);post('ready');
</script></body></html>
<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; media-src blob:"><style>
html,body{margin:0;height:100%;background:#F7F4EE;font:14px sans-serif;color:#2B211A}button{width:100%;height:100%;border:1px solid #EDE7DC;border-radius:10px;background:#F2EADA;color:#2B211A;font-size:14px;touch-action:none;user-select:none}button.recording{background:#D1AB69}button.cancel{background:#B9514D;color:white}
</style></head><body><button id="hold" aria-label="按住说话">按住说话</button><script>
const button=document.getElementById('hold'),post=x=>window.jdj?.postMessage(typeof x==='string'?x:JSON.stringify(x));
let pressed=false,cancelled=false,busy=false,recorder,stream,started=0,origin=0,epoch=0,timeout,timer;
function release(){clearTimeout(timeout);clearInterval(timer);stream?.getTracks().forEach(t=>t.stop());stream=null;button.className='';button.textContent='按住说话';}
window.setSending=value=>{busy=value;button.disabled=value;};
window.stopMedia=()=>{pressed=false;epoch++;if(recorder?.state==='recording'){recorder.onstop=null;recorder.stop();}release();};
window.permissionReady=async()=>{if(!pressed||busy||stream)return;const stamp=++epoch;try{const capture=await navigator.mediaDevices.getUserMedia({audio:true,video:false});if(!pressed||stamp!==epoch){capture.getTracks().forEach(t=>t.stop());return;}stream=capture;const mime='audio/webm;codecs=opus';if(!MediaRecorder.isTypeSupported(mime))throw Error('请更新 Android System WebView 后使用语音');const chunks=[];recorder=new MediaRecorder(stream,{mimeType:mime,audioBitsPerSecond:64000});
recorder.ondataavailable=e=>{if(e.data.size)chunks.push(e.data);};recorder.onstop=async()=>{const stamp=epoch,short=performance.now()-started<600;const discard=cancelled||short;release();if(discard){post({type:'status',message:short?'说话时间太短':'已取消语音'});return;}window.setSending(true);const blob=new Blob(chunks,{type:'audio/webm'}),bytes=new Uint8Array(await blob.arrayBuffer());if(stamp!==epoch){window.setSending(false);return;}post({type:'start'});for(let p=0;p<bytes.length;p+=90000){let s='';for(const b of bytes.subarray(p,p+90000))s+=String.fromCharCode(b);post({type:'chunk',data:btoa(s)});}post({type:'complete'});};
recorder.start(250);started=performance.now();button.className='recording';button.textContent='松开发送 · 上滑取消';post({type:'status',message:'正在录音,松开发送,上滑取消'});timer=setInterval(()=>{if(!cancelled)button.textContent=Math.ceil((performance.now()-started)/1000)+' 秒 · 松开发送';},500);timeout=setTimeout(()=>{pressed=false;recorder.stop();},59500);
}catch(e){release();post({type:'status',message:e.message});}};
button.onpointerdown=e=>{if(busy)return;e.preventDefault();button.setPointerCapture(e.pointerId);pressed=true;cancelled=false;origin=e.clientY;post({type:'permission'});};
button.onpointermove=e=>{if(!pressed)return;cancelled=origin-e.clientY>65;if(recorder?.state==='recording'){button.className=cancelled?'cancel':'recording';button.textContent=cancelled?'松开取消':'松开发送 · 上滑取消';}};
function end(e,cancel){e.preventDefault();pressed=false;if(cancel)cancelled=true;if(recorder?.state==='recording')recorder.stop();else{epoch++;release();}}
button.onpointerup=e=>end(e,false);button.onpointercancel=e=>end(e,true);window.addEventListener('pagehide',window.stopMedia);post('ready');
</script></body></html>