是否可以更改 MediaRecorder 的流?

Is it possible to change MediaRecorder's stream?

getUserMedia(constrains).then(stream => {
    var recorder = new MediaRecorder(stream)
})

recorder.start()
recorder.pause()
// get new stream getUserMedia(constrains_new)
// how to update recorder stream here?
recorder.resume()

可能吗?我尝试创建 MediaStream 并使用 addTrackremoveTrack 方法来更改流轨道但没有成功(当我尝试使用更新流)

有什么想法吗?

简短的回答是否定的,这是不可能的。 MediaStream recording spec explicitly describes this behavior: https://w3c.github.io/mediacapture-record/#dom-mediarecorder-start。该算法的要点 15.3 表示 "If at any point, a track is added to or removed from stream’s track set, the UA MUST immediately stop gathering data ...".

但如果您只想录制音频,您可以使用 AudioContext 来代理您的流。创建一个 MediaStreamAudioDestinationNode 并使用它提供的流进行录制。然后,您可以将带有 MediaStreamAudioSourceNodes and/or MediaStreamTrackAudioSourceNodes 的流提供到音频图中,并以您想要的任何方式混合它们。

最后但同样重要的是,目前有计划将您正在寻找的功能添加到规范中。也许你只需要稍等一下。或者可能会更长一些,具体取决于您使用的浏览器。 :-)

https://github.com/w3c/mediacapture-record/issues/167

https://github.com/w3c/mediacapture-record/pull/186