大网数据广告活动
Web 标准与前端开发

AudioSession: state property

· 52CE 我爱测

The **`state`** read-only property of the AudioSession interface returns the current state of the audio session.

Audio Session API

The state read-only property of the AudioSession interface returns the current state of the audio session.

Value

A string whose value is one of the following:

  • "active"
    • : The audio session is currently playing sound, recording audio, or both.
  • "interrupted"
    • : The audio session was active but has been temporarily suspended by the platform, for example because of an incoming phone call or another application taking exclusive control of audio. The session may return to "active" once the interruption ends.
  • "inactive"
    • : The audio session is not playing or recording audio, and is not currently interrupted. This is the default state.

Description

The platform updates the state in response to the page's own audio activity (starting or stopping playback or recording) and to platform-level events, such as another application taking exclusive control of audio or an incoming phone call. The AudioSession.statechange_event event can be monitored for notifications when the value changes.

The property reflects the state of every audio source and sink that the page has created, including audio and video elements, AudioContext instances, and microphone MediaStreamTracks obtained with MediaDevices.getUserMedia(). The session becomes "active" when any of these is producing or capturing audible sound, and returns to "inactive" when all of them are stopped. Starting or stopping an individual source doesn't otherwise change the reported state.

When the state becomes "interrupted", because something else has taken exclusive control of audio, the browser automatically pauses playing media elements, suspends AudioContexts, and mutes microphone tracks that belong to the session. It then automatically resumes them when the state returns to "active". Note that this only applies to elements that are producing audible output when the state is interrupted; a muted or silent element is never affected and must be explicitly paused or resumed if needed. See the AudioSession.statechange_event event page for an example.

The value of state doesn't stop you calling HTMLMediaElement.play() or other mechanisms to start audio, but such a call will likely fail if another app is still holding exclusive control of audio. In this case the state stays "inactive" or "interrupted", the method returns without error, but no sound may actually be produced. You should instead wait for the AudioSession.statechange_event event to report that state has returned to "active" on its own.

Examples

Basic usage

navigator.audioSession.addEventListener("statechange", () => {
  statusElement.textContent = `Audio session: ${navigator.audioSession.state}`;
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。