我正在尝试加载一个页面,它是网络广播的听众页面。此页面适用于 python aiortc 和 RTCPeerConnection。
我正在尝试使用 QWebKit pyqt5 模块加载此页面,但屏幕中出现此错误:
https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js line 2: jQuery.Deferred exception: Can't find variable: RTCPeerConnection
https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js line 2: ReferenceError: Can't find variable: RTCPeerConnection
该页面的javascript代码是:
文件:radio.js
// peer connection
var pc = null;
var radio_on = false;
function createPeerConnection() {
var config = {
sdpSemantics: 'unified-plan',
iceServers: [{urls: ['stun:stun.l.google.com:19302']}]
};
pc = new RTCPeerConnection(config);
// connect audio
pc.addEventListener('track', function(evt) {
if (evt.track.kind == 'audio'){
document.getElementById('audio').srcObject = evt.streams[0];
var promise = document.getElementById('audio').play();
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
}).catch(error => {
// Autoplay was prevented.
// Show a "Play" button so that user can start playback.
});
}
};
});
pc.onconnectionstatechange = function(event) {
switch(pc.connectionState) {
case "connected":
// The connection has become fully connected
console.log("Connected to radio server.");
break;
case "disconnected":
console.log("Disconnected from radio server. Try for reconnect.");
setTimeout(function(){ start(); }, 3000);
break;
case "failed":
// One or more transports has terminated unexpectedly or in an error
console.log("Connection failed. Try for reconnect.");
setTimeout(function(){ start(); }, 3000);
break;
case "closed":
// The connection has been closed
console.log("Connection closed. Try for reconnect.");
setTimeout(function(){ start(); }, 3000);
break;
}
}
return pc;
}
function start_radio_stream(){
document.getElementById("audio").play();
document.body.removeEventListener("mousemove", start_radio_stream, true);
}
function negotiate() {
return pc.createOffer({offerToReceiveAudio:true}).then(function(offer) {
return pc.setLocalDescription(offer);
}).then(function() {
// wait for ICE gathering to complete
return new Promise(function(resolve) {
console.log(pc.iceGatheringState);
if (pc.iceGatheringState === 'complete') {
resolve();
} else {
function checkState() {
console.log(pc.iceGatheringState);
if (pc.iceGatheringState === 'complete') {
pc.removeEventListener('icegatheringstatechange', checkState);
resolve();
}
}
pc.addEventListener('icegatheringstatechange', checkState);
}
});
}).then(function() {
var offer = pc.localDescription;
return fetch('/offer', {
body: JSON.stringify({
sdp: offer.sdp,
type: offer.type
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
});
}).then(function(response) {
return response.json();
}).then(function(answer) {
return pc.setRemoteDescription(answer);
}).catch(function(e) {
alert(e);
console.log(e);
});
}
function start() {
pc = createPeerConnection();
return negotiate();
}
$(document).ready(function(){
start();
})
html代码是:
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- own javascript -->
<script src="radio.js"></script>
</head>
<body>
<audio style="width:100%;margin-top:5mm;background:rgb(241,243,244)" id="audio" autoplay="true" controls="true"></audio>
</body>
</html>
我认为这不是什么大麻烦,而且很容易解决。
编辑:我删除了 jquery 包含并$(document).ready
使用纯 javascript 代码进行更新,但存在错误:
http://192.168.1.8:8080/radio.js line 10: ReferenceError: Can't find variable: RTCPeerConnection
请注意,我不能使用QWebEngine
,QWebKit
因为它在 msys2 终端中不可用。:\
也许我必须使用像 selenium、phantomjs、geckodriver 这样的替代品,但我不知道如何将它嵌入到 pyqt5 应用程序框架中。