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

AudioContext: createMediaStreamTrackSource() method

· 52CE 我爱测

The **`createMediaStreamTrackSource()`** method of the AudioContext interface creates and returns a MediaStreamTrackAudioSourceNode which represents an audio source whose data comes from the specified MediaStreamTrack.

Web Audio API

The createMediaStreamTrackSource() method of the AudioContext interface creates and returns a MediaStreamTrackAudioSourceNode which represents an audio source whose data comes from the specified MediaStreamTrack.

This differs from AudioContext.createMediaStreamSource, which creates a MediaStreamAudioSourceNode whose audio comes from the audio track in a specified MediaStream whose MediaStreamTrack.id is first, lexicographically (alphabetically).

Syntax

createMediaStreamTrackSource(track)

Parameters

  • track
    • : The MediaStreamTrack to use as the source of all audio data for the new node.

Return value

A MediaStreamTrackAudioSourceNode object which acts as a source for audio data found in the specified audio track.

Examples

In this example, MediaDevices.getUserMedia is used to request access to the user's microphone. Once that access is attained, an audio context is established and a MediaStreamTrackAudioSourceNode is created using createMediaStreamTrackSource(), taking its audio from the first audio track in the stream returned by getUserMedia().

Then a BiquadFilterNode is created using BaseAudioContext/createBiquadFilter, and it's configured as desired to perform a lowshelf filter on the audio coming from the source. The output from the microphone is then routed into the new biquad filter, and the filter's output is in turn routed to the audio context's BaseAudioContext/destination.

navigator.mediaDevices
  .getUserMedia({ audio: true, video: false })
  .then((stream) => {
    audio.srcObject = stream;
    audio.onloadedmetadata = (e) => {
      audio.play();
      audio.muted = true;
    };

    const audioCtx = new AudioContext();
    const audioTracks = stream.getAudioTracks();
    const source = audioCtx.createMediaStreamTrackSource(audioTracks[0]);

    const biquadFilter = audioCtx.createBiquadFilter();
    biquadFilter.type = "lowshelf";
    biquadFilter.frequency.value = 3000;
    biquadFilter.gain.value = 20;

    source.connect(biquadFilter);
    biquadFilter.connect(audioCtx.destination);
  })
  .catch((err) => {
    // Handle getUserMedia() error
  });

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

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

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