capture.requestMic()
通过可信宿主 UI 申请麦克风。成功返回会话级只读句柄。
Syntax
const stream = await host.capture.requestMic(constraints, options?);
Parameters
constraints: MicrophoneConstraints 使用模块页的封闭结构;对象必填、字段可选。options?: CallOptions 可取消等待中的可信 UI;成功后的实际格式在 MicrophoneStream 中返回并在会话内不变。
maxChannels 是硬上限,preferredSampleRate 是偏好;实际采样率必须在 [8000, 192000] 内。返回的 maxFramesPerRead 恰好等于宿主公布的 capture.maxAudioFramesPerRead。
Return value
MicrophoneStream | null
Examples
recordButton.onclick = async () => {
const stream = await host.capture.requestMic({ preferredSampleRate: 48_000, maxChannels: 1 });
if (stream) beginRecording(stream);
};
Errors
特有错误:denied、activation-required、device-unavailable、cancelled、limit-exceeded。只有平台提供能与权限拒绝可靠区分的可信选择器时,用户在该选择器中关闭才返回 null;浏览器把关闭和拒绝都报告为权限拒绝时统一返回 denied。调用方取消使用 cancelled。
Requirements
- 能力:
host:capture.requestMic - 可信用户激活和系统确认:必须
- 可取消:是;调用方取消返回
cancelled - 信任档位:yellow