我在触发RTCPeerConnection.createOffer()时遇到下一个错误:
抛出:DOMException
代码:11
columnNumber :0
数据:null
文件名:""
lineNumber :0
消息:"如果没有有效的收发器,则无法创建优惠。"
名称:" InvalidStateError"
结果:2152923147
堆叠:""
__ proto __ :DOMExceptionPrototype {name:Getter,message:Getter,INDEX_SIZE_ERR:1,...} index.js:176:3
接下来是完整的代码片段(错误由行触发: pc.createOffer()。然后(... ):
let pc = {};
let localStream = {};
const btnStart = document.querySelector("button[id='btnstart']");
let localVideoDisplay = document.getElementById('vOwn');
btnStart.addEventListener("click",(e)=>{
pc = new RTCPeerConnection();
pc.onicecandidate = (event)=>{
console.log(event);
}
/*
It captures local media
*/
navigator.mediaDevices.getUserMedia({ audio: 1, video: 1 }).then((stream)=>{
localStream = stream;
localVideoDisplay.srcObject = localStream;
}).catch((err)=>{
console.log(err);
});
/*
It creates sdpOffer
*/
pc.createOffer().then((rtcSession)=>{
console.log(rtcSession);
}).catch((error)=>{
console.log(error);
});
});

<button id="btnstart">Start</button>
<video id="vOwn" autoplay></video>
&#13;
代码在Chrome(移动和桌面)和Safari(移动版v11.3.1和桌面版v11.0.2)上完美运行我的问题是:这是一个firefox错误还是我做错了什么?
答案 0 :(得分:3)
在添加任何流或创建数据通道之前,您正在调用pc.createOffer()
。
这通常不是你想要的。试试这个:
navigator.mediaDevices.getUserMedia({ audio: 1, video: 1 }).then((stream)=>{
localStream = stream;
localVideoDisplay.srcObject = localStream;
stream.getTracks().forEach((t) => pc.addTrack(t, stream));
return pc.createOffer()
})
.then((rtcSession)=>{
console.log(rtcSession);
}).catch((error)=>{
console.log(error);
});