call.html :
content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
视频通话
{{toName}}
正在努力呼叫...
var app, rong, startPlayTime;
apiready = function apiready() {
rong = api.require('rongCloud2');
/*
rong.removeVideoView({ //by_zilai
userId: $api.getStorage('uid')+ '',
});
*/
app = new Vue({
el: '#app',
data: {
uid: $api.getStorage('uid') + '',
toId: api.pageParam.toId + '',
toName: api.pageParam.toName,
toAvatar: api.pageParam.toAvatar,
publishId: api.pageParam.publish_id,//'' by_zilai ,一定有这个ID
waitSeconds:60,//等待60秒后,当作noAnswer处理
needTimer:1,//需要定时器
hungUpIng:0,//1表示正在挂断处理中,阻止监听事件触发的后续挂断动作
},
created: function created() {},
mounted: function mounted() {
var _this = this
// this.openRemoteFrame()
// this.openLocalFrame()
_this.startCall()
_this.sendCallMessage()
_this.timeOutHangup()
if (api.systemType != 'ios') {//进入页面开始振铃
_this.playRing()
}
$event.addEvent('videoHangupTo', function (ret) {//被叫方非人工挂断
if(_this.hungUpIng==0){_this.hungUpIng=1;_this.notConnected('answer_fail')}
})
$event.addEvent('noAnswer', function (ret) {//被叫方未接听
if(_this.hungUpIng==0){_this.hungUpIng=1;_this.notConnected('answer_timeout')}
})
$event.addEvent('videoHangupToClick', function (ret) {//被叫方人为挂断 ,
if(_this.hungUpIng==0){_this.hungUpIng=1;_this.notConnected('answer_refused')}
})
$event.addEvent('videoHangupMe', function (ret) {//主叫方非人工挂断
if(_this.hungUpIng==0){_this.hungUpIng=1;_this.notConnected('call_fail')}
})
/*本页面是主叫方人为挂断动作的发起页面,无需监听videoHangupMeClick事件*/
$event.keyback(function (res) {//主叫方人为挂断,退出页面?
if(_this.hungUpIng==0){_this.hungUpIng=1;_this.notConnected('call_cancel')}
})
$event.addEvent('callFrm', function (ret) {
console.log('accpeting_signal_received');
console.log('接收到了');
_this.needTimer=0;//启动定时器自毁
api.stopPlay()
$event.sendEvent('callSuccess', {
toId: api.pageParam.toId + ''
}) //改变选择通话的为已通话
$event.openWin('video_call', {
toId: api.pageParam.toId + '',
toName: api.pageParam.toName,
toAvatar: api.pageParam.toAvatar,
publish_id: api.pageParam.publish_id,
videoType: api.pageParam.videoType,//1免费
orderType: api.pageParam.orderType,//有可能值为空
time:api.pageParam.time,
},{
slidBackEnabled: false
})
})
},
methods: {
// 振铃
playRing: function playRing() {
var _this = this
api.startPlay({
path: 'widget://res/ring.mp3'
}, function (ret, err) {
if (ret) {
//startPlayTime = setTimeout(function(ret) {
_this.playRing() //自循环
// },500)
}
})
},
//发消息
sendCallMessage:function sendCallMessage() {
var _this = this
var tempExtra = {
fromId:_this.uid,
fromName: $api.getStorage('uname'),
fromAvatar: $api.getStorage('uavatar'),
toId: _this.toId,
toName: _this.toName,
toAvatar: _this.toAvatar,
theme: 'call'
}
$r.sendText(_this.toId, '视频通话邀请...', tempExtra)
},
//定时器
timeOutHangup: function timeOutHangup() {
var _this5 = this;
this.myInterval=setInterval(function () {
innerthis=this;
if(_this5.needTimer==0){//能够自我销毁
clearInterval(innerthis.myInterval);//下面的挂断照照常执行,并不因为clear动作,不执行本次循环
innerthis.myInterval = null;
return; //不往下面执行了
}
if(_this5.waitSeconds==0){
if(_this5.hungUpIng==0){_this5.hungUpIng=1;_this5.notConnected('answer_timeout')}
clearInterval(innerthis.myInterval);//下面的因为clear动作,不执行往下执行本次循环
innerthis.myInterval = null;
}
_this5.waitSeconds--;
}, 1000);
},
//接通电话
startCall: function startCall() {
var _this = this
var extra = {
toName: $api.getStorage('uname'),
toAvatar: $api.getStorage('uavatar'),
toId: $api.getStorage('uid'),
publish_id: api.pageParam.publish_id,
videoType: api.pageParam.videoType//1免费,0收费
}
//console.log(JSON.stringify(extra), '888')
extra = JSON.stringify(extra)
// rong.setConnectionStatusListener(function(ret, err) {
// api.toast({ msg: ret.result.connectionStatus });
// });
rong.startCall({
targetId: _this.toId,
mediaType: 'video',
conversationType: 'PRIVATE',
extra: extra,
userIdList: [_this.toId]
}, function (ret) {
console.log('call_sended')
$event.sendEvent('rongVideo') //发送融云监听事件
if (api.systemType == 'ios') {
rong.addCallSessionListener({
target: 'didConnect'
}, function (ret) {
$event.sendEvent('callFrm')
})
}
console.log(JSON.stringify(ret)); //示例:{"callSession":{"callId":"NHbG6C759Tg_vqic9HZWpg"}}
})
},
//未接通挂断
notConnected: function notConnected(options) {
var _this = this
api.stopPlay()
var tempExtra = {
fromId: $api.getStorage('uid'),
fromName: $api.getStorage('uname'),
fromAvatar: $api.getStorage('uavatar'),
toId: api.pageParam.toId + '',
toName: api.pageParam.toName,
toAvatar: api.pageParam.toAvatar,
theme: 'call'
}
if(options=='call_cancel'){
rong.hangup()
//监听到对方已经通过融云rong.hangup()方法,那么不重复发送挂断,会造成震荡
//监听到自己和对方方非人为断线,也不要重复发送挂断,会造成震荡
}
if(options=='call_cancel'){//人为发起的
$r.sendText(this.toId, '视频邀请已取消', tempExtra)//被叫方无行为
}
axios('messageVideoAcceptingSender', {//等待接听阶段统一用这个接口进行挂断 messageVideoAccept
publish_id: _this.publishId,//added by_zilai
to_uid: _this.toId,
connected:'no',
options: options,
}, function (res) {
//console.log(JSON.stringify(res))
if(res.code==1 && res.data && res.data.processed_by_other==1){
//表示该挂断事件由对方已经处理了,不重复发消息了
}else{
if(options=='call_fail'){//监听事件发起
$r.sendText(api.pageParam.toId, '视频发起失败', tempExtra)//被叫方无行为
}
if(options=='answer_timeout'){//监听事件发起,接听方的answer_fail最终转化为主叫方的answer_timeout
$r.sendText(api.pageParam.toId, '等待接听超时', tempExtra)//被叫方无行为,tangjun
}
}
if(res.code==1){
$event.closeWin('call')
}else{
$event.toast(res.msg);
}
})
},
}
})
}