Javascript:<audio>.captureStream() 是否在没有 play() 的情况下工作?
Javascript: Does <audio>.captureStream() work without play()?
在浏览器中,我想捕获以 .mp3 作为源的音频标签流,然后通过 WebRTC 将其实时发送到服务器。我不想通过扬声器听到它。
是否可以在没有扬声器输出的情况下调用 audioElement.play()?
似乎可以将音频元素静音并仍然捕获流:
audioElement.muted = true;
var stream = audioElement.captureStream();
new Audio()
returns 连接到浏览器默认音频输出设备的 HTMLAudioElement
。您可以通过 运行ning:
在开发控制台中验证这一点
> new Audio().sinkId
<- ""
其中空字符串输出指定 user agent default sinkId
.
一种将 HTMLAudioElement
实例的输出连接到非默认接收器的灵活方式(例如,如果您不想通过扬声器听到它,而只是想将它发送到另一个目的地就像 Audio
对象中的 WebRTC peer connection), is to use the global AudioContext
object to create a new MediaStreamAudioDestinationNode
. Then you can grab the MediaElementAudioSourceNode
通过 audioContext.createMediaElementSource(mp3Audio)
保存您的 mp3 文件,并将其连接到新的音频目标节点。然后,当您 运行 mp3Audio.play()
, 它只会流式传输到目标节点,而不是默认(扬声器)音频输出。
完整示例:
// Set up the audio node source and destination...
const mp3FilePath = 'testAudioSample.mp3'
const mp3Audio = new Audio(mp3FilePath)
const audioContext = new AudioContext()
const mp3AudioSource = audioContext.createMediaElementSource(mp3Audio)
const mp3AudioDestination = audioContext.createMediaStreamDestination()
mp3AudioSource.connect(mp3AudioDestination)
// Connect the destination track to whatever you want,
// e.g. another audio node, or an RTCPeerConnection.
const mp3AudioTrack = mp3AudioDestination.stream.getAudioTracks()[0]
const pc = new RTCPeerConnection()
pc.addTrack(track)
// Prepare the `Audio` instance playback however you'd like.
// For example, loop it:
mp3Audio.loop = true
// Start streaming the mp3 audio to the new destination sink!
await mp3Audio.play()
在浏览器中,我想捕获以 .mp3 作为源的音频标签流,然后通过 WebRTC 将其实时发送到服务器。我不想通过扬声器听到它。
是否可以在没有扬声器输出的情况下调用 audioElement.play()?
似乎可以将音频元素静音并仍然捕获流:
audioElement.muted = true;
var stream = audioElement.captureStream();
new Audio()
returns 连接到浏览器默认音频输出设备的 HTMLAudioElement
。您可以通过 运行ning:
> new Audio().sinkId
<- ""
其中空字符串输出指定 user agent default sinkId
.
一种将 HTMLAudioElement
实例的输出连接到非默认接收器的灵活方式(例如,如果您不想通过扬声器听到它,而只是想将它发送到另一个目的地就像 Audio
对象中的 WebRTC peer connection), is to use the global AudioContext
object to create a new MediaStreamAudioDestinationNode
. Then you can grab the MediaElementAudioSourceNode
通过 audioContext.createMediaElementSource(mp3Audio)
保存您的 mp3 文件,并将其连接到新的音频目标节点。然后,当您 运行 mp3Audio.play()
, 它只会流式传输到目标节点,而不是默认(扬声器)音频输出。
完整示例:
// Set up the audio node source and destination...
const mp3FilePath = 'testAudioSample.mp3'
const mp3Audio = new Audio(mp3FilePath)
const audioContext = new AudioContext()
const mp3AudioSource = audioContext.createMediaElementSource(mp3Audio)
const mp3AudioDestination = audioContext.createMediaStreamDestination()
mp3AudioSource.connect(mp3AudioDestination)
// Connect the destination track to whatever you want,
// e.g. another audio node, or an RTCPeerConnection.
const mp3AudioTrack = mp3AudioDestination.stream.getAudioTracks()[0]
const pc = new RTCPeerConnection()
pc.addTrack(track)
// Prepare the `Audio` instance playback however you'd like.
// For example, loop it:
mp3Audio.loop = true
// Start streaming the mp3 audio to the new destination sink!
await mp3Audio.play()