html页面添加
<video id="remoteVideo" class="corner" autoplay playsinline style="display: none;"></video>
<video id="localVideo" class="full" muted autoplay playsinline style="display: none;"></video>
<script src="https://sqimg.qq.com/expert_qq/webrtc/3.0/WebRTCAPI.min.js"></script>
<script>
$(function(){
WebRTCAPI.fn.detectRTC({
screenshare : false
},
function(info){
console.log(info);
if( !info.isTBS ) {
// alert('不支持TBS')
}
// alert(info.TBSversion)
if( !info.support ) {
// alert('不支持WebRTC')
}
else{
//alert('支持WebRTC')
}
});
var RTC = new WebRTCAPI({
"userId": '<%=openid%>',
"userSig": '<%=userSig%>',
"sdkAppId": ,
"accountType": 36862,
"closeLocalMedia": false
},
function(){
RTC.openAudio();
RTC.openVideo();
RTC.enterRoom({
roomid :<%=roomid%>,
role : 'OnlineVideo',
},function()
{// alert("创建房间成功")
RTC.startRTC({
role: 'OnlineVideo',
}, function(){
//成功
//alert('成功推流')
},function(){
//失败
});
});
},function ( error ){
console.error( error )
alert("初始化失败");
});
// webRTC 部分
//重复登录被T
function onKickout() {
console.error("on kick out!");
}
//服务器超时
function onRelayTimeout(msg) {
console.error("onRelayTimeout!" + (msg ? JSON.stringify(msg) : ""));
}
//本地流新增
function onLocalStreamAdd(info) {
// alert("bendi01");
var videoElement = document.getElementById("localVideo");
videoElement.srcObject = info.stream;
// soundMeter(info);
}
function soundMeter( info ){
console.debug(' meter start')
// 分析音频流
var meter = WebRTCAPI.SoundMeter({
stream: info.stream,
onprocess: function( data ){
if (data.volume>0.01) {
alert(data.status+","+data.volume)
}
}
})
setTimeout( function(){
console.debug(' meter stop')
meter.stop();
},10000);
}
//远端流新增/更新
function onRemoteStreamUpdate(info) {
document.getElementById('footvoice').style.display = "block";
console.log("onRemoteStreamAdd!");
<%if (calltype != "voice")
{ %>
document.getElementById('webrtc-toggle').style.display = "block";
$("#tbs-nick").hide();
$(".picture").hide();
$("#remoteVideo").show();
$("#localVideo").show();
<%}
else
{%>
//$("#voice-wait").show();
//关闭本地摄像头
RTC.closeVideo();
<%}%>
document.title = '正在服务中';
$("#webrtc-timing").show();
var videoElement = document.getElementById("remoteVideo");
videoElement.srcObject =info.stream;
}
//远端流断开
function onRemoteStreamRemove( info ) {
console.log( info.userId+ ' 断开了连接');
var videoNode = document.getElementById( info.videoId );
if( videoNode ){
videoNode.srcObject = null;
document.getElementById(info.videoId).parentElement.removeChild(videoNode);
}
}
function onWebSocketClose() {
RTC.quit();
}
//视频流事件通知。
RTC.on( 'onStreamNotify' , function( info ){
if (info.event=='inactive') {
//alert("音视频流断开");
onWebSocketClose();
}
})
// 远端流新增/更新
RTC.on("onRemoteStreamUpdate",onRemoteStreamUpdate)
// 本地流新增
RTC.on("onLocalStreamAdd",onLocalStreamAdd)
// 远端流断开
RTC.on("onRemoteStreamRemove",onRemoteStreamRemove)
// 重复登录被T
RTC.on("onKickout",onKickout)
// 服务器超时
RTC.on("onRelayTimeout",onRelayTimeout)
RTC.on("onErrorNotify", function( info ){
console.error( info )
//alert(info.errorMsg);
});
</script>
</body>
</html>