我想做的事情:
我已经使用WebRtc API通过浏览器访问音频,
现在我想做更多的事情:
1)当我打电话到达电话答录机时,我想通过单击留下预先录制的语音邮件。
所以我想要,我只是通过麦克风流音频文件,但是我没有怎么做,
this.audioTest = function() {
console.log("Testing function");
var a1 = document.getElementById("remote");
var pc = this.session.sessionDescriptionHandler.peerConnection;
var backgroundMusic = audioContext.createMediaElementSource(document.getElementById("remoteMedia"));
var mixedOutput = audioContext.createMediaStreamDestination();
backgroundMusic.connect(mixedOutput);
console.log(mixedOutput);
var remoteStream = new MediaStream();
pc.getSenders().forEach(function (sender) {
var track = mixedOutput.stream;
if (track) {
remoteStream.addTrack(track);
}
});
a1.srcObject = remoteStream;
}
这是我的代码,我先播放音频,然后尝试通过麦克风流式传输音频, 但是没有任何领先 如果有人可以帮助或建议我任何事情,那太好了。
答案 0 :(得分:0)
您将要使用Web Audio API。在那里,您可以在麦克风和其他来源的音频之间无缝混合(或切换),同时仅将一个MediaStream发送到WebRTC对等方。
相关方法是上下文中的createMediaStreamDestination
。 https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createMediaStreamDestination
const rtcOutput = audioContext.createMediaStreamDestination();
yourMixOutput.conntect(rtcOutput);
然后,在发送给对等方的邮件中使用rtcOutput.stream
。
答案 1 :(得分:0)
我认为您不需要通过麦克风流音频文件(语音消息)。您可以将流(传递到WebRTC)从麦克风切换为语音消息。
const micStream; // user MediaStream
const msgStream; // voice message MediaStream
const pc; //RTCPeerConnection
const destination = audioCtx.createMediaStreamDestination();
const micSource = audioCtx.createMediaStreamSource(micStream);
const micGainNode = audioCtx.createGain(); //with this you can change volume of stream
micSource.connect(micGainNode);
micGainNode.connect(destination);
// adding stream of destination to WebRTC.
// note that stream is taken from destination
destination.stream.getTracks().forEach(
function (track) {
pc.addTrack(track,destination.stream);
}
);
// then on send voice message listener
sendVoiceMessageListener = function(){
//we need to disconnect microphone stream and connect voice message stream
micGainNode.disconnect(destination);
const msgSource = audioCtx.createMediaStreamSource(msgStream);
const msgGainNode = audioCtx.createGain();
micSource.connect(micGainNode);
micGainNode.connect(destination);
}