| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- cc.Class({
- extends: cc.BaseClass,
- properties: {
- // m_atlas: cc.SpriteAtlas
- },
- ctor: function () {
- this.m_LoadArr = new Array(); //加载队列
- this.m_WaitArr = new Array(); //等待播放
- this.m_SelfArr = new Array(); //本地上传
- this.m_PlayingArr = new Array(); //
- this.m_PlayingIndex = INVALD_CHAIR; //播放中
- this.m_GameEngine = null;
- this.m_Voices = new Array(); // 语音队列
- this.m_Sequence = new Array();
- this.statRecordDelayTime = 0;
- this.isStartRecord = false;
- this.touchTime = 0;
- this.isTouching = false;
- this.recordTime = 20;
- //
- this.sendCount = 0;
- this.receivCount = 0;
- },
- onLoad: function () {
- this.m_btVoice = this.$('Voice/btVoice');
- this.m_nRecording = this.$('Voice/Recording');
- this.m_nSign = this.$('Voice/Sign');
- //按钮监听
- this.m_btVoice.on(cc.Node.EventType.TOUCH_START, function (event) {
- if (this.statRecordDelayTime > 0) return;
- if (this.isStartRecord) return;
- this.statRecordDelayTime = 1.1;
- console.log("TOUCH_START--1");
- this.touchTime = 0.7;
- //this.RecordStart();
- this.isTouching = true;
- }.bind(this), this.m_btVoice);
- this.m_btVoice.on(cc.Node.EventType.TOUCH_END, function (event) {
- this.isTouching = false;
- if (this.isStartRecord == false) return;
- console.log("录音停止");
- this.RecordOver();
- this.isStartRecord = false;
- }.bind(this), this.m_btVoice);
- this.m_btVoice.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
- this.isTouching = false;
- if (this.isStartRecord == false) return;
- console.log("录音停止");
- this.RecordOver();
- this.isStartRecord = false;
- }.bind(this), this.m_btVoice);
- for (var i in this.m_nSign.children) {
- this.m_PlayingArr[i] = this.m_nSign.children[i].getComponent(cc.Sprite);
- }
- this.schedule(this.PlayingTag, 0.2);
- },
- PlayingTag: function () {
- if (this.m_PlayingIndex == INVALD_CHAIR) return;
- if (this.m_PlayingTag == null) this.m_PlayingTag = 1;
- this.m_PlayingTag++;
- if (this.m_PlayingTag > 3) this.m_PlayingTag = 0;
- // this.m_PlayingArr[this.m_PlayingIndex].spriteFrame = this.m_atlas.getSpriteFrame('Play' + this.m_PlayingTag);
- cc.gPreLoader.LoadRes('Image_Play' + this.m_PlayingTag, 'Chat_2', function (sf, Param) {
- var Index = Param.Index;
- // var Tag = Param.m_PlayingTag;
- this.m_PlayingArr[Index].spriteFrame = sf;
- }.bind(this), {
- Index: this.m_PlayingIndex,
- Tag: this.m_PlayingTag
- });
- },
- //初始化
- InitVoice: function (Hook) {
- if (this.m_GameEngine != null) return;
- this.m_Hook = Hook;
- this.m_GameEngine = this.m_Hook.m_GameClientEngine;
- this.statRecordDelayTime = 0;
- this.isStartRecord = false;
- this.touchTime = 0;
- if (!ThirdPartyVoiceInit()) this.node.active = false;
- },
- RecordStart: function () {
- if (this.m_nRecording.active) return;
- if (this.m_PlayingIndex != INVALD_CHAIR) return this.ShowTips('請等待語音播放結束後再進行錄音!');
- cc.audioEngine.pauseAll();
- this.ShowRecording(true);
- // this.m_RecordPath = ThirdPartyVoiceOnRecord(this);
- // if (!cc.sys.isBrowser) {
- // if (this.m_RecordPath == "") return this.ShowTips("没有录音权限!!!")
- // this.ShowRecording(true);
- // }
- ThirdPartyStartRecord();
- },
- PlayVoice: function (data) {
- if (this.m_Voices[data.dwSendUserID] == null) this.m_Voices[data.dwSendUserID] = '';
- this.m_Voices[data.dwSendUserID] += data.szVID;
- if (data.byPlatform == 0) {
- data.szVID = this.m_Voices[data.dwSendUserID];
- data.szVID = data.szVID.replace(/\n/g, '');
- this.m_Voices[data.dwSendUserID] = '';
- this.m_Sequence.push(data);
- this.receivCount++;
- console.log("声音传回完结0", this.receivCount)
- if (this.isPlaying) return;
- this.OnPlayFinish();
- } else {
- this.receivCount++;
- console.log("声音传回没完结1", this.receivCount)
- }
- },
- onRecordFinish: function (data) {
- if (data == '') {
- cc.audioEngine.resumeAll();
- this.ShowRecording(false);
- this.ShowTips('錄製時間過短!');
- return;
- }
- var str = data;
- while (str.length != 0) {
- if (str.length > 255) {
- this.m_GameEngine.OnSendUserVoice(2, str.slice(0, 255));
- str = str.slice(255);
- } else {
- this.m_GameEngine.OnSendUserVoice(0, str.slice(0));
- str = '';
- }
- }
- },
- RecordOver: function () {
- if (this.m_RecordStart == null) return
- this.m_RecordTime = new Date().getTime() - this.m_RecordStart;
- this.m_RecordStart = null;
- let str = ThirdPartyStopRecord((this.m_RecordTime < 1000 ? '0' : '1'));
- console.log("RecordOver--1", str.length)
- if (cc.sys.isNative) {
- if (cc.sys.OS_ANDROID == cc.sys.os) {
- if (str == '') {
- cc.audioEngine.resumeAll();
- this.ShowRecording(false);
- this.ShowTips('錄製時間過短!');
- return;
- }
- this.sendCount = 0;
- this.receivCount = 0;
- //todo
- //this.m_Voices[g_GlobalUserInfo.GetGlobalUserData().dwUserID] = '';
- while (str.length != 0) {
- if (str.length > 2559) {
- this.m_GameEngine.OnSendUserVoice_A(2, str.slice(0, 2559));
- str = str.slice(2559);
- this.sendCount++;
- } else {
- this.m_GameEngine.OnSendUserVoice_A(0, str.slice(0));
- str = '';
- this.sendCount++;
- console.log("发送声音count: ", this.sendCount)
- }
- // if (str.length > 255) {
- // this.m_GameEngine.OnSendUserVoice(2, str.slice(0, 255));
- // str = str.slice(255);
- // this.sendCount++;
- // } else {
- // this.m_GameEngine.OnSendUserVoice(0, str.slice(0));
- // str = '';
- // this.sendCount++;
- // console.log("发送声音count: ", this.sendCount)
- // }
- // if (str.length > 255) {
- // setTimeout((s) => {
- // this.m_GameEngine.OnSendUserVoice(2, s);
- // }, 40, str.slice(0, 255))
- // str = str.slice(255);
- // this.sendCount++;
- // } else {
- // setTimeout((s) => {
- // this.m_GameEngine.OnSendUserVoice(0, s);
- // }, 40, str.slice(0))
- // str = '';
- // this.sendCount++;
- // console.log("发送声音count: ", this.sendCount)
- // }
- }
- this.ShowRecording(false);
- } else {
- this.ShowRecording(false);
- }
- cc.audioEngine.resumeAll();
- }
- console.log("RecordOver--2")
- // ThirdPartyVoiceStopRecord(this);
- // ThirdPartyVoiceOnUpload(this, this.m_RecordPath);
- // if(cc.sys.isBrowser) {
- // var self = this;
- // var voice = {
- // localId: '',
- // serverId: '',
- // viewID: 65535,
- // };
- // wx.stopRecord({
- // success: function (res1) {
- // voice.localId = res1.localId;
- // wx.uploadVoice({
- // localId: voice.localId,
- // success: function (res2) {
- // voice.serverId = res2.serverId;
- // self.OnRecordOver(voice);
- // }
- // });
- // }
- // });
- // } else {
- // }
- },
- //发送完成回调
- OnRecordOver: function (RecordStr) {
- this.ShowRecording(false);
- if (cc.sys.isBrowser) {
- //添加无需加载队列
- RecordStr.viewID = GameDef.MYSELF_VIEW_ID;
- this.m_SelfArr.push(RecordStr);
- //发送语音通知
- this.m_GameEngine.OnSendUserVoice(1, RecordStr.serverId);
- } else {
- var RecordArr = RecordStr.split(',');
- //添加无需加载队列
- RecordArr[2] = GameDef.MYSELF_VIEW_ID;
- this.m_SelfArr.push(RecordArr);
- //发送语音通知
- this.m_GameEngine.OnSendUserVoice(0, RecordArr[0]);
- }
- this.CheckPlayArr();
- },
- OnLoadVoice: function (RecordStr) {
- if (cc.sys.isBrowser) {
- for (var i in this.m_LoadArr) {
- if (this.m_LoadArr[i].serverId == RecordStr.serverId) {
- this.m_LoadArr[i].localId = RecordStr.localId;
- this.m_WaitArr.push(this.m_LoadArr.splice(i, 1)[0]);
- break
- }
- }
- } else {
- var RecordArr = RecordStr.split(',');
- for (var i in this.m_LoadArr) {
- if (this.m_LoadArr[i][0] == RecordArr[0]) {
- this.m_LoadArr[i][1] = RecordArr[1];
- this.m_WaitArr.push(this.m_LoadArr.splice(i, 1)[0]);
- break
- }
- }
- }
- this.CheckPlayArr();
- },
- // OnPlayFinish: function () {
- // this.ShowPlaying(INVALD_CHAIR);
- // this.CheckPlayArr();
- // },
- OnPlayFinish: function () {
- if (this.m_Sequence.length > 0) {
- var data = this.m_Sequence.shift();
- if (!this.isPlaying) {
- this.isPlaying = true;
- cc.audioEngine.pauseAll();
- }
- var wChairID = this.m_GameEngine.GetUserChairID(data.dwSendUserID);
- var wViewID = this.m_GameEngine.SwitchViewChairID(wChairID);
- this.ShowPlaying(wViewID);
- ThirdPartyPlayVoice(data.szVID);
- } else {
- this.isPlaying = false;
- cc.audioEngine.resumeAll();
- this.ShowPlaying(INVALD_CHAIR);
- }
- },
- CheckPlayArr: function () {
- if (this.m_PlayingIndex != INVALD_CHAIR || this.m_nRecording.active) return;
- if (cc.sys.isBrowser) {
- if (this.m_WaitArr.length > 0) {
- var playInfo = (this.m_WaitArr.splice(0, 1)[0]);
- var self = this;
- //播放音频文件
- wx.playVoice({
- localId: playInfo.localId,
- success: function () {
- self.ShowPlaying(playInfo.viewID);
- }
- });
- wx.onVoicePlayEnd({
- complete: function (res) {
- self.OnPlayFinish();
- }
- });
- }
- } else {
- if (this.m_WaitArr.length > 0) {
- var playInfo = this.m_WaitArr.splice(0, 1)[0];
- //播放音频文件
- ThirdPartyPlayVoice(playInfo[1]);
- //显示播放标志
- this.ShowPlaying(playInfo[2]);
- }
- }
- },
- //显示播放标志
- ShowPlaying: function (ViewID) {
- this.m_PlayingIndex = ViewID;
- for (var i = 0; i < this.m_PlayingArr.length; i++) {
- this.m_PlayingArr[i].node.active = (i == ViewID);
- }
- if (ViewID != INVALD_CHAIR) {
- var scalex = this.m_Hook.m_UserVoiceArr[ViewID].x >= 200 ? -1 : 1;
- this.m_PlayingArr[ViewID].node.setPosition(this.m_Hook.m_UserVoiceArr[ViewID].x, this.m_Hook.m_UserVoiceArr[ViewID].y);
- this.m_PlayingArr[ViewID].node.setScale(scalex, 1);
- this.m_TempBGM = cc.gSoundRes.GetBGMVolume();
- window.SaveSetting(window.SetKey_Music, 0);
- } else {
- window.SaveSetting(window.SetKey_Music, this.m_TempBGM);
- }
- //音量调整
- cc.gSoundRes.UpdateVolume();
- },
- //显示录制
- ShowRecording: function (bShow) {
- this.m_nRecording.active = bShow;
- if (bShow) {
- //开始计时
- this.m_RecordStart = new Date().getTime();
- this.m_TempBGM = cc.gSoundRes.GetBGMVolume();
- window.SaveSetting(window.SetKey_Music, 0);
- } else {
- window.SaveSetting(window.SetKey_Music, this.m_TempBGM);
- }
- //音量调整
- cc.gSoundRes.UpdateVolume();
- },
- //播放消息处理
- AddVoice: function (pVoice) {
- if (pVoice.byPlatform == 1) { //WX
- for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
- if (this.m_Hook.m_pIClientUserItem[i] == null) continue;
- if (this.m_Hook.m_pIClientUserItem[i].GetUserID() == pVoice.dwSendUserID) {
- if (i == GameDef.MYSELF_VIEW_ID) {
- for (var j = 0; j < this.m_SelfArr.length; j++) {
- if (this.m_SelfArr[j].serverId == pVoice.szVID) {
- this.m_SelfArr[j].viewID = i;
- this.m_WaitArr.push(this.m_SelfArr.splice(j, 1)[0]);
- break;
- }
- }
- } else {
- this.m_LoadArr.push({
- serverId: pVoice.szVID,
- localId: '',
- viewID: i
- });
- var self = this;
- wx.downloadVoice({
- serverId: pVoice.szVID,
- success: function (res) {
- self.OnLoadVoice({
- serverId: pVoice.szVID,
- localId: res.localId,
- viewID: i
- });
- }
- });
- }
- break;
- }
- }
- } else {
- for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
- if (this.m_Hook.m_pIClientUserItem[i] == null) continue;
- if (this.m_Hook.m_pIClientUserItem[i].GetUserID() == pVoice.dwSendUserID) {
- if (i == GameDef.MYSELF_VIEW_ID) { //本地消息
- for (var j = 0; j < this.m_SelfArr.length; j++) {
- if (this.m_SelfArr[j][0] == pVoice.szVID) {
- this.m_WaitArr.push(this.m_SelfArr.splice(j, 1)[0]);
- break;
- }
- }
- } else { //网络消息加载
- this.m_LoadArr.push([pVoice.szVID, '', i]);
- //下载音频文件
- ThirdPartyGVoiceLoadVoice(pVoice.szVID);
- }
- break;
- }
- }
- }
- this.CheckPlayArr();
- },
- update: function (dt) {
- this.statRecordDelayTime -= dt;
- if (this.statRecordDelayTime < 0) {
- this.statRecordDelayTime = 0;
- }
- if (this.isTouching) {
- this.touchTime -= dt;
- if (this.touchTime < 0) {
- this.touchTime = 0;
- if (this.isStartRecord == false) {
- this.RecordStart();
- this.isStartRecord = true;
- this.recordTime = 20;
- console.log("录音开始");
- }
- }
- }
- if (this.isStartRecord) {
- this.recordTime -= dt;
- if (this.recordTime < 0 && this.isTouching) {
- this.RecordOver();
- this.isStartRecord = false;
- this.isTouching = false;
- console.log("录音");
- }
- }
- if (this.m_nRecording == null) return
- //录音动画
- if (this.m_nRecording.active) {
- var Now = new Date().getTime();
- if (this.m_RecordStart != null && Now - this.m_RecordStart > 59000) {
- this.RecordOver();
- this.ShowTips('錄音時長達到上限!');
- }
- }
- },
- });
|