javascript - 歌剧浏览器上未调用 webrtc onicecandidate
问题描述
我有一个问题,我的 onececandidate 事件没有在 Opera 浏览器中被触发,这是我的代码
我试过getUserMedia以不同的方式(通过addStream和addTrack)将流添加到RTCPeerConnection,设置offerToReceiveAudio和offerToReceiveVideo,这些都没有帮助
代码
const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
const RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription;
const RTCIceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
const mediaConstraints = {
offerToReceiveAudio: true,
offerToReceiveVideo: false
}
const options = {
iceServers: [
{
urls: 'turn:74.11.232.11:3478',
credential: 'password',
username: 'username'
}
],
};
function getUserMedia(callback) {
navigator.mediaDevices.getUserMedia({
audio: true,
video: false
}).then(callback);
}
getUserMedia((stream) => {
localStream = stream
localStream.getAudioTracks()[0].enabled = false
WebRtcPeerConnection = new RTCPeerConnection(options)
WebRtcPeerConnection.addStream(stream)
WebRtcPeerConnection.createOffer(mediaConstraints).then((offer) => {
console.log(offer.sdp)
WebRtcPeerConnection.setLocalDescription(offer);
WebRtcPeerConnection.onicecandidate = onRoomIceCandidate
onRoomOffer(null, offer)
});
})
解决方案
我的错误是声明了常量 RTCPeerConnection,正如我在 Opera 浏览器中理解的那样 RTCPeerConnection 是一个全局类(不在 window.RTCPeerConnection 等中)我需要替换这一行
const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
对此
const RTCPeerConnection = RTCPeerConnection || window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
推荐阅读
- asynchronous - 强制同步循环http请求
- c++ - dllexport 类模板实例(专业化),减少仅头文件模板库的编译时间
- javascript - 如何在 React Native 的右下角放置浮动操作按钮
- html - 跨行和列连接表格中的某些单元格
- svelte - 使用汇总的 sapper 客户端构建中的外部和全局变量
- html - Bootstrap 4 马赛克布局
- python - Python value_count() 逻辑运算符
- java - Android MVVM 架构和观察来自 API 的数据变化
- python-3.x - 元组索引超出范围 - 训练音频模型
- spring - Spring中如何实现客户端之间的非实时聊天