简体   繁体   English

Web 音频音频上下文 createMediaStreamSource 口吃

[英]Web Audio audiocontext createMediaStreamSource stuttering

I want to mix different audio media streams in to one stream.我想将不同的音频媒体流混合到一个 stream 中。 I'm been doing this with Web Audio audiocontext and createMediaStreamSource.我一直在用 Web Audio audiocontext 和 createMediaStreamSource 来做这个。

But the final mixed audio is stuttering.但最终的混合音频很卡顿。

Have anyone an idea how to optimize this to avoid stuttering?有谁知道如何优化它以避免口吃?

// init audio context
var audioContext = new AudioContext({ latencyHint: 0 });
var audioDestination = audioContext.createMediaStreamDestination();

// add audio streams
audioContext.createMediaStreamSource(audioStream1).connect(audioDestination);
audioContext.createMediaStreamSource(audioStream2).connect(audioDestination);
audioContext.createMediaStreamSource(audioStream3).connect(audioDestination);
audioContext.createMediaStreamSource(audioStream4).connect(audioDestination);

// get mixed audio stream tracks
var audioTrack = audioDestination.stream.getTracks()[0];

// get video track
var videoTrack = videoStream.getTracks()[0];

// combine video and audio tracks into single stream.
var finalStream = new MediaStream([videoTrack, audioTrack]);

// assign to video element
el_video.srcObject = finalStream;

You could try setting the latencyHint to 'playback' like this:您可以尝试将latencyHint设置为'playback'如下所示:

const audioContext = new AudioContext({ latencyHint: 'playback' });

This allows the browser to add a bit of latency to the audio graph which can help on underpowered devices.这允许浏览器向音频图添加一些延迟,这有助于功率不足的设备。 Setting the latencyHint to 0 on the other hand will tell the browser that it should do things as fast as possible which increases the risk of dropouts.另一方面,将latencyHint设置为0会告诉浏览器它应该尽可能快地执行操作,这会增加退出的风险。

Having said that, the latencyHint is only a hint.话虽如此, latencyHint只是一个提示。 The browser may very well ignore it.浏览器很可能会忽略它。 You can check what the browser is actually doing by inspecting the baseLatency property.您可以通过检查baseLatency属性来检查浏览器实际在做什么。

console.log(audioContext.baseLatency);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM