XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; media-src blob: data:"><style>
body{font:15px sans-serif;background:#F7F4EE;color:#2B211A;margin:12px;text-align:center}video{width:100%;max-height:56vh;border-radius:14px;background:#2B211A}button{border:0;border-radius:12px;background:#D1AB69;color:#2B211A;padding:12px 18px;margin:5px;font-size:14px}button:disabled{opacity:.5}#time{font-size:30px;padding:12px}#error{color:#B9514D}#facing{font-size:12px;color:#938A7D;margin:8px}
</style></head><body><video id="preview" autoplay muted playsinline></video><audio id="audio" controls hidden></audio><div id="facing"></div><button id="switch" hidden>切换摄像头</button><div id="time">00:00</div><div id="error"></div><button id="start">开始录制</button><button id="finish" hidden>完成录制</button><canvas id="surface" hidden></canvas><script>
const kind='__KIND__',video=kind==='VIDEO',max=video?29.5:59.5,preview=document.getElementById('preview'),audio=document.getElementById('audio'),surface=document.getElementById('surface'),switchButton=document.getElementById('switch'),startButton=document.getElementById('start'),finishButton=document.getElementById('finish'),post=x=>window.jdj?.postMessage(typeof x==='string'?x:JSON.stringify(x));
let camera,sound,output,recorder,blob,timer,timeout,paint,startTime,createdAt,url,facing='environment',cameraId='',epoch=0,switching=false,starting=false,sending=false,stopped=false;
function stopTracks(stream){stream?.getTracks().forEach(t=>t.stop());}
function release(){clearInterval(timer);clearInterval(paint);clearTimeout(timeout);stopTracks(camera);stopTracks(sound);stopTracks(output);camera=sound=output=null;preview.srcObject=null;}
function error(e){document.getElementById('error').textContent=e.message;post({type:'status',message:e.message});}
window.setSending=value=>{sending=value;startButton.disabled=value||starting||switching;switchButton.disabled=value||switching;};
window.stopMedia=()=>{stopped=true;epoch++;if(recorder?.state==='recording'){recorder.onstop=null;recorder.stop();}release();blob=null;if(url)URL.revokeObjectURL(url);url=null;preview.pause();audio.pause();startButton.hidden=false;finishButton.hidden=true;switchButton.disabled=false;post({type:'reset'});};
async function openCamera(change=false){if(!video||switching||stopped)return;switching=true;startButton.disabled=switchButton.disabled=true;const stamp=epoch,previousFacing=facing,previousId=cameraId;try{
 const devices=(await navigator.mediaDevices.enumerateDevices()).filter(d=>d.kind==='videoinput');let constraints={facingMode:{ideal:facing},width:{ideal:640},height:{ideal:480},frameRate:{ideal:20,max:24}};
 if(change){facing=facing==='environment'?'user':'environment';const labelled=devices.find(d=>d.deviceId!==cameraId&&(facing==='environment'?/back|rear|后置|environment/i:/front|前置|user/i).test(d.label));const next=labelled||devices.find(d=>d.deviceId!==cameraId);constraints=next?{...constraints,deviceId:{exact:next.deviceId}}:{...constraints,facingMode:{exact:facing}};}
 stopTracks(camera);camera=null;let captured;try{captured=await navigator.mediaDevices.getUserMedia({audio:false,video:constraints});}catch(e){
  if(!change)throw e;if(stopped||stamp!==epoch)return;facing=previousFacing;
  captured=await navigator.mediaDevices.getUserMedia({audio:false,video:previousId?{deviceId:{exact:previousId},width:{ideal:640},height:{ideal:480}}:{facingMode:{ideal:previousFacing},width:{ideal:640},height:{ideal:480}}});error(Error('暂未切换摄像头,已保留原摄像头'));
 }
 if(stopped||stamp!==epoch){stopTracks(captured);return;}camera=captured;const settings=camera.getVideoTracks()[0].getSettings();cameraId=settings.deviceId||'';facing=settings.facingMode||facing;preview.src='';preview.srcObject=camera;preview.controls=false;preview.muted=true;preview.style.transform=facing==='user'?'scaleX(-1)':'';await preview.play();document.getElementById('facing').textContent=facing==='user'?'前置摄像头':'后置摄像头';switchButton.hidden=false;
 }finally{switching=false;startButton.disabled=sending||starting;switchButton.disabled=sending;}}
function draw(){if(camera&&preview.readyState>=2)surface.getContext('2d').drawImage(preview,0,0,surface.width,surface.height);}
async function start(){if(switching||starting||sending||stopped)return;starting=true;startButton.disabled=true;const stamp=epoch;try{post({type:'reset'});blob=null;document.getElementById('error').textContent='';audio.hidden=true;const mime=video?'video/webm;codecs=vp8,opus':'audio/webm;codecs=opus';if(!MediaRecorder.isTypeSupported(mime))throw Error('请更新 Android System WebView 后录制');
 if(video&&!camera)await openCamera();if(stopped||stamp!==epoch)return;const captured=await navigator.mediaDevices.getUserMedia({audio:true,video:false});if(stopped||stamp!==epoch){stopTracks(captured);return;}sound=captured;
 if(video){surface.width=640;surface.height=480;draw();paint=setInterval(draw,50);output=new MediaStream([...surface.captureStream(20).getVideoTracks(),...sound.getAudioTracks()]);}else output=sound;
 const chunks=[];recorder=new MediaRecorder(output,{mimeType:mime,videoBitsPerSecond:900000,audioBitsPerSecond:64000});recorder.ondataavailable=e=>{if(e.data.size)chunks.push(e.data);};recorder.onstop=()=>{epoch++;release();blob=new Blob(chunks,{type:video?'video/webm':'audio/webm'});if(url)URL.revokeObjectURL(url);url=URL.createObjectURL(blob);preview.style.transform='';if(video){preview.src=url;preview.controls=true;preview.muted=false;}else{audio.src=url;audio.hidden=false;}finishButton.hidden=true;startButton.hidden=false;startButton.textContent='重新录制';switchButton.hidden=true;post({type:'recorded',createdAt});};
 createdAt=new Date().toISOString();recorder.start(250);startTime=performance.now();timer=setInterval(()=>{document.getElementById('time').textContent='00:'+String(Math.floor((performance.now()-startTime)/1000)).padStart(2,'0');},200);timeout=setTimeout(finish,max*1000);startButton.hidden=true;finishButton.hidden=false;post({type:'status',message:video?'正在录制,可切换前后摄像头;30 秒内自动完成':'正在录音,60 秒内自动完成'});
 }catch(e){release();error(e);}finally{starting=false;startButton.disabled=sending;}}
function finish(){if(recorder?.state==='recording'){epoch++;recorder.stop();}}
window.exportRecording=async()=>{if(!blob)return;post({type:'start'});const bytes=new Uint8Array(await blob.arrayBuffer());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'});};
startButton.onclick=start;finishButton.onclick=finish;switchButton.onclick=()=>openCamera(true).catch(error);window.addEventListener('pagehide',window.stopMedia);preview.hidden=!video;post('ready');if(video)openCamera().catch(error);
</script></body></html>