admin管理员组文章数量:1429037
I have a webRTC connection established with audio and video.
On the caller side, I'd like to change the audio input.
e.g. the User changes the audioinput
from a dropdown list.
What's the workflow to substitute the audio track of an existing stream?
Can I add another audio track and make one active over the other? how?
Looks like I may need to call getUserMedia
again passing constraints (?), which to my understanding es to create a New mediaStream
instances and not modify the existing.
I have a webRTC connection established with audio and video.
On the caller side, I'd like to change the audio input.
e.g. the User changes the audioinput
from a dropdown list.
What's the workflow to substitute the audio track of an existing stream?
Can I add another audio track and make one active over the other? how?
Looks like I may need to call getUserMedia
again passing constraints (?), which to my understanding es to create a New mediaStream
instances and not modify the existing.
- 1 Modify the MediaStream in an active peerconnection, the peerconnection object will fire an onnegotiationneeded event. Handle that event and re-exchange SDPs. – Sourav Ghosh Commented Oct 8, 2016 at 8:20
- thx. Could you detail this in as an Answer below (sequence of functions to call). I think this will create a new MediaStream with a different ID right? So that wouldn't be changing the audio track of an existing MediaStream (keeping the same ID) but rather renegociating the connection with a new MediaStream that will contain the same video + a different audio track. – zabumba Commented Oct 9, 2016 at 23:26
- stackoverflow./questions/56944864/… – bain Commented Feb 9, 2021 at 16:58
2 Answers
Reset to default 3There is now a much simpler API for this operation: RTCRtpSender.replaceTrack().
It could look something like this:
const currentSenders = peerConnection.getSenders();
const currentAudioSender = currentSenders.find((s) => s.track.kind === 'audio');
currentAudioSender.replaceTrack(newAudioTrack);
For us it looks something like this:
const replaceTrack = async (peerConnection, oldSender, track, stream) => {
peerConnection.removeTrack(oldSender);
const newSender = peerConnection.addTrack(track, stream);
const localSdp = await peerConnection.createOffer({ offerToReceiveAudio: 1 });
await peerConnection.setLocalDescription(reply);
const response = await sendOffer(peerConnection.localDescription);
const description = new RTCSessionDescription(response);
peerConnection.setRemoteDescription(description);
return newSender;
}
本文标签: javascriptWebRTCHow to change the audio track for a existing streamStack Overflow
版权声明:本文标题:javascript - WebRTC - How to change the audio track for a existing stream - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745533207a2662154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论