pubgv3/assets/scripts/tools/wxVoice.js
2022-05-31 16:44:54 +08:00

236 lines
6.3 KiB
JavaScript

// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var SDKManage = require("SDKManage")
var NetManage = require("NetManage")
var wxVoice = cc.Class({
init(){
},
starGameRecorder(){
if(cc.recorder_switch && this.vedio){
this.vedio.start({
hookBgm:true,
bitrate:3000,
}).then((res) => {
console.log(res);
});
}
},
stopGameRecorder(){
if(cc.recorder_switch && this.vedio){
this.vedio.stop().then((res) => {
cc.Notifier.emit("Grstop",res)
//console.log(res);
});
}
},
play(url){
var self = this
"undefined" != typeof wx && wx.downloadFile({
url: url,
fail: function(e) {
},
success: function(res) {
if (200 == res.statusCode) {
self.playSound(res.tempFilePath,1)
}
}
});
},
onRecordEnd(res){
var self = this
this.fs.readFile({
filePath: res.tempFilePath,
success: function(res) {
wx.request({
url: self.url + "?c=Voice&a=upload&account_id=" + SDKManage.account_id +"&session_id="+ NetManage.session_id+"&rd=" + new Date().getTime(),
method: "POST",
data: res.data,
header: {
"content-type": "application/octet-stream"
},
fail: function(e) {
},
success: function(e) {
if(e.statusCode==200&&e.data.errcode==0){
cc.gameMgr&&cc.gameMgr.sendVoice(e.data.download_url)
}
}
});
}
});
//test
// this.audio[this.audiopos].src = res.tempFilePath
// this.audio[this.audiopos].play()
// this.audiopos++;
},
dostart:function(){
this.rec && this.rec.start({
sampleRate: 11025,
numberOfChannels: 1,
encodeBitRate: 16e3,
format: "mp3",
frameSize: 128
})
},
start: function() {
if("undefined" == typeof wx){
return
}
var self = this
self.dostart();
// wx.getSetting({
// success(res) {
// if (!res.authSetting['scope.record']) {
// wx.authorize({
// scope: 'scope.record',
// success() {
// self.dostart();
// }
// })
// }
// else{
// self.dostart();
// }
// }
// })
},
stop: function() {
this.rec && this.rec.stop()
},
doteamTalk(){
wx.joinVoIPChat({
signature:this.signature,
groupId:this.groupId,
nonceStr:this.nonceStr,
timeStamp:this.timeStamp,
success:function(res){
cc.gameMgr.uic.changeTeamMode()
},
fail:function(res){
},
})
},
exitTeamTalk(){
if("undefined" == typeof wx){
return
}
wx.exitVoIPChat&&wx.exitVoIPChat()
},
teamTalk(data){
if("undefined" == typeof wx){
return
}
if(!wx.joinVoIPChat){
return
}
this.signature = data.sign
this.groupId = data.groupId
this.nonceStr = data.nonceStr
this.timeStamp = data.timeStamp
this.doteamTalk()
//var self = this
// wx.getSetting({
// success(res) {
// if (!res.authSetting['scope.record']) {
// wx.authorize({
// scope: 'scope.record',
// success() {
// self.doteamTalk();
// }
// })
// }
// else{
// self.doteamTalk();
// }
// }
// })
},
openTeamVoice(){
if("undefined" == typeof wx){
return
}
wx.updateVoIPChatMuteConfig&&wx.updateVoIPChatMuteConfig({
muteConfig:{
muteMicrophone:false,
muteEarphone:false
}
})
},
closeTeamVoice(){
if("undefined" == typeof wx){
return
}
wx.updateVoIPChatMuteConfig&&wx.updateVoIPChatMuteConfig({
muteConfig:{
muteMicrophone:true,
muteEarphone:true
}
})
},
xiuzhengbgm(){
if(this.bgm){
var sv = 1
if(cc.notSound){
sv=0
}
cc.audioEngine.setMusicVolume(sv)
}
},
playbgm(url){
var self = this
cc.loader.loadRes("sounds/"+url, cc.AudioClip, null, function (err, clip) {
if (!err) {
self.audioID = cc.audioEngine.playMusic(clip, true);
}
});
},
stopbgm(){
cc.audioEngine.stopMusic();
},
playSound:function(url,soundVolume){
cc.loader.loadRes("sounds/"+url, cc.AudioClip, null, function (err, clip) {
if (!err) {
cc.audioEngine.playEffect(clip, false);
}
});
},
});
var wr = new wxVoice();
wr.init()
module.exports = wr;