我在项目中使用JSSIP与星号。主题是我不jnow如何从浏览器中的Asterisk播放音频答案。现在我正在尝试这个
myPhone.on('newRTCSession', function(data){
var session = data.session;
session.on('peerconnection', function(e){
remoteAudio.src = window.URL.createObjectURL(e.stream);
remoteAudio.play();
});
});
但没有触发事件,我也尝试了事件" AddStream",但是正如我所知,在我当前的JSSIP版本(3.2.4(中已将其删除
连接到星号是可以的。我开始呼叫,接收答案,然后星号必须播放音频文件,然后终止呼叫。连接8秒后将终止,但是音频没有播放。这是RTCSession.connection.remoteDescription.sdp
v=0
o=- 857805013 857805013 IN IP4 127.0.0.1
s=-
t=0 0
a=msid-semantic: WMS
m=audio 19006 RTP/SAVPF 0 8 126
c=IN IP4 95.47.143.134
a=rtcp:19007 IN IP4 95.47.143.134
a=candidate:Hc0a80068 1 udp 2130706431 192.168.0.104 19006 typ host generation 0
a=candidate:S5f2f8f86 1 udp 1694498815 95.47.143.134 19006 typ srflx raddr 192.168.0.104 rport 19006 generation 0
a=candidate:Hc0a80068 2 udp 2130706430 192.168.0.104 19007 typ host generation 0
a=candidate:S5f2f8f86 2 udp 1694498814 95.47.143.134 19007 typ srflx raddr 192.168.0.104 rport 19007 generation 0
a=ice-ufrag:218863545319313f5ed15c9b0503a7f1
a=ice-pwd:5e1dc0457efadffe13b44666585eecb9
a=fingerprint:sha-256 56:EE:4C:B8:78:88:AB:A4:C2:72:84:94:15:BE:7C:6E:D4:BD:2F:21:F1:F6:6D:68:E8:91:14:DC:94:72:75:0C
a=setup:active
a=mid:audio
a=sendrecv
a=rtcp-mux
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:126 telephone-event/8000
a=maxptime:150
a=ptime:20
有一个称为 connection
的属性,该属性在RTCSession
实例中包含RTCPeerConnection
实例。据我所知,它代表遥控/其他部分。
因此,这里是一个代码示例,它显示了如何添加事件侦听器以捕获远程部分的媒体流并使用它。
session.connection.addEventListener('addstream', function (e) {
// set remote audio stream
const remoteAudio = document.createElement('audio');
remoteAudio.src = window.URL.createObjectURL(e.stream);
remoteAudio.play();
});
对于来电,session.connection
未立即实例化。因此,您需要附上另一个事件侦听器peerconnection
,以如您在问题中提到的那样抓住它。包括该部分,它应该如下;
session.on('peerconnection', function(data) {
data.peerconnection.addEventListener('addstream', function (e) {
// set remote audio stream
const remoteAudio = document.createElement('audio');
remoteAudio.src = window.URL.createObjectURL(e.stream);
remoteAudio.play();
});
});
这里有一些链接,可以帮助您更好地理解它;
- jssip.rtcsession.connection
- rtcpeerconnection.onaddstream
addstream
事件现在已弃用。您应该使用track
事件。示例实现:
attachRemoteStream(session) {
// Fetch the existing audio element or create a new one
const remoteAudio = document.getElementById('remoteAudio')
session.connection.ontrack = (event) => {
if (event.track.kind === 'audio') {
if (event.streams.length > 0) {
remoteAudio.srcObject = event.streams[0];
} else {
const stream = new MediaStream([event.track]);
remoteAudio.srcObject = stream;
}
}
};
}
,然后进行来电,请在peerconnection
上拨打该功能:
session.on('peerconnection', (e) => {
attachRemoteStream(session);
});