- 规则引擎:提子、禁自杀、全局同形(超级劫)、中国规则数子(白贴 7.5 目)、停一手终局 - 服务端:axum + tokio,房间/观战/包干计时/断线重连/语音中继,前端资源内嵌单文件 - 前端:Canvas 木纹棋盘(9/13/19 路、星位、四边坐标、翻转视角)、WebAudio 合成落子声、 本机双人模式、终局数子面板 - 测试:16 组规则断言 + 5 组 e2e(提子/劫/数子/观战/语音/静态资源) - 构建缓存遵循 dev-cache-layout:target/ 留项目内并忽略,前端零构建不产生包缓存
137 lines
3.6 KiB
JavaScript
137 lines
3.6 KiB
JavaScript
/**
|
||
* 语音互动:录音(MediaRecorder)+ base64 编解码 + 播放
|
||
*
|
||
* 约束:单条最长 30 秒;编码优先 webm/opus(Chrome/Firefox/Edge),
|
||
* Safari 自动落到 audio/mp4。跨内核播放能力取决于对方浏览器的解码支持。
|
||
*/
|
||
|
||
export const MAX_SECONDS = 30;
|
||
|
||
const MIME_CANDIDATES = [
|
||
'audio/webm;codecs=opus',
|
||
'audio/webm',
|
||
'audio/mp4',
|
||
'audio/ogg;codecs=opus',
|
||
];
|
||
|
||
export function isSupported() {
|
||
return !!(
|
||
typeof window !== 'undefined' &&
|
||
window.MediaRecorder &&
|
||
navigator.mediaDevices &&
|
||
typeof navigator.mediaDevices.getUserMedia === 'function'
|
||
);
|
||
}
|
||
|
||
function pickMimeType() {
|
||
if (typeof MediaRecorder === 'undefined') return '';
|
||
for (const t of MIME_CANDIDATES) {
|
||
try {
|
||
if (MediaRecorder.isTypeSupported(t)) return t;
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
return '';
|
||
}
|
||
|
||
export class Recorder {
|
||
constructor() {
|
||
this.stream = null;
|
||
this.mr = null;
|
||
this.chunks = [];
|
||
this.mime = '';
|
||
this.startedAt = 0;
|
||
this.recording = false;
|
||
}
|
||
|
||
async start() {
|
||
if (this.recording) return;
|
||
this.stream = await navigator.mediaDevices.getUserMedia({
|
||
audio: {
|
||
echoCancellation: true,
|
||
noiseSuppression: true,
|
||
autoGainControl: true,
|
||
},
|
||
});
|
||
const mime = pickMimeType();
|
||
const opts = mime ? { mimeType: mime, audioBitsPerSecond: 32000 } : {};
|
||
this.mr = new MediaRecorder(this.stream, opts);
|
||
this.mime = this.mr.mimeType || mime || 'audio/webm';
|
||
this.chunks = [];
|
||
this.mr.ondataavailable = (e) => {
|
||
if (e.data && e.data.size > 0) this.chunks.push(e.data);
|
||
};
|
||
this.startedAt = performance.now();
|
||
this.mr.start();
|
||
this.recording = true;
|
||
}
|
||
|
||
/** 停止录音,返回 { blob, mime, durationMs };没录到东西返回 null */
|
||
async stop() {
|
||
if (!this.recording || !this.mr) return null;
|
||
const dur = Math.min(MAX_SECONDS * 1000, performance.now() - this.startedAt);
|
||
const mr = this.mr;
|
||
const stopped = new Promise((resolve) => {
|
||
mr.onstop = () => resolve();
|
||
// 有些实现 onstop 迟迟不来,兜底
|
||
setTimeout(resolve, 800);
|
||
});
|
||
try {
|
||
mr.stop();
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
await stopped;
|
||
this.recording = false;
|
||
this.release();
|
||
const blob = new Blob(this.chunks, { type: this.mime || 'audio/webm' });
|
||
this.chunks = [];
|
||
if (!blob.size) return null;
|
||
return { blob, mime: this.mime || blob.type || 'audio/webm', durationMs: Math.round(dur) };
|
||
}
|
||
|
||
/** 放弃这次录音(手指划走、页面切走等) */
|
||
cancel() {
|
||
try {
|
||
if (this.mr && this.recording) this.mr.stop();
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
this.recording = false;
|
||
this.chunks = [];
|
||
this.release();
|
||
}
|
||
|
||
release() {
|
||
if (this.stream) {
|
||
this.stream.getTracks().forEach((t) => t.stop());
|
||
this.stream = null;
|
||
}
|
||
this.mr = null;
|
||
}
|
||
}
|
||
|
||
/** Blob -> base64(不带 data: 前缀) */
|
||
export function blobToBase64(blob) {
|
||
return new Promise((resolve, reject) => {
|
||
const fr = new FileReader();
|
||
fr.onload = () => {
|
||
const s = String(fr.result || '');
|
||
const comma = s.indexOf(',');
|
||
resolve(comma >= 0 ? s.slice(comma + 1) : '');
|
||
};
|
||
fr.onerror = () => reject(fr.error || new Error('read failed'));
|
||
fr.readAsDataURL(blob);
|
||
});
|
||
}
|
||
|
||
/** base64 -> Blob URL(记得 revoke) */
|
||
export function base64ToUrl(b64, mime) {
|
||
const bin = atob(b64);
|
||
const len = bin.length;
|
||
const bytes = new Uint8Array(len);
|
||
for (let i = 0; i < len; i++) bytes[i] = bin.charCodeAt(i);
|
||
return URL.createObjectURL(new Blob([bytes], { type: mime || 'audio/webm' }));
|
||
}
|