VoiceCtrl.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties: {
  4. // m_atlas: cc.SpriteAtlas
  5. },
  6. ctor: function () {
  7. this.m_LoadArr = new Array(); //加载队列
  8. this.m_WaitArr = new Array(); //等待播放
  9. this.m_SelfArr = new Array(); //本地上传
  10. this.m_PlayingArr = new Array(); //
  11. this.m_PlayingIndex = INVALD_CHAIR; //播放中
  12. this.m_GameEngine = null;
  13. this.m_Voices = new Array(); // 语音队列
  14. this.m_Sequence = new Array();
  15. this.statRecordDelayTime = 0;
  16. this.isStartRecord = false;
  17. this.touchTime = 0;
  18. this.isTouching = false;
  19. this.recordTime = 20;
  20. //
  21. this.sendCount = 0;
  22. this.receivCount = 0;
  23. },
  24. onLoad: function () {
  25. this.m_btVoice = this.$('Voice/btVoice');
  26. this.m_nRecording = this.$('Voice/Recording');
  27. this.m_nSign = this.$('Voice/Sign');
  28. //按钮监听
  29. this.m_btVoice.on(cc.Node.EventType.TOUCH_START, function (event) {
  30. if (this.statRecordDelayTime > 0) return;
  31. if (this.isStartRecord) return;
  32. this.statRecordDelayTime = 1.1;
  33. console.log("TOUCH_START--1");
  34. this.touchTime = 0.7;
  35. //this.RecordStart();
  36. this.isTouching = true;
  37. }.bind(this), this.m_btVoice);
  38. this.m_btVoice.on(cc.Node.EventType.TOUCH_END, function (event) {
  39. this.isTouching = false;
  40. if (this.isStartRecord == false) return;
  41. console.log("录音停止");
  42. this.RecordOver();
  43. this.isStartRecord = false;
  44. }.bind(this), this.m_btVoice);
  45. this.m_btVoice.on(cc.Node.EventType.TOUCH_CANCEL, function (event) {
  46. this.isTouching = false;
  47. if (this.isStartRecord == false) return;
  48. console.log("录音停止");
  49. this.RecordOver();
  50. this.isStartRecord = false;
  51. }.bind(this), this.m_btVoice);
  52. for (var i in this.m_nSign.children) {
  53. this.m_PlayingArr[i] = this.m_nSign.children[i].getComponent(cc.Sprite);
  54. }
  55. this.schedule(this.PlayingTag, 0.2);
  56. },
  57. PlayingTag: function () {
  58. if (this.m_PlayingIndex == INVALD_CHAIR) return;
  59. if (this.m_PlayingTag == null) this.m_PlayingTag = 1;
  60. this.m_PlayingTag++;
  61. if (this.m_PlayingTag > 3) this.m_PlayingTag = 0;
  62. // this.m_PlayingArr[this.m_PlayingIndex].spriteFrame = this.m_atlas.getSpriteFrame('Play' + this.m_PlayingTag);
  63. cc.gPreLoader.LoadRes('Image_Play' + this.m_PlayingTag, 'Chat_2', function (sf, Param) {
  64. var Index = Param.Index;
  65. // var Tag = Param.m_PlayingTag;
  66. this.m_PlayingArr[Index].spriteFrame = sf;
  67. }.bind(this), {
  68. Index: this.m_PlayingIndex,
  69. Tag: this.m_PlayingTag
  70. });
  71. },
  72. //初始化
  73. InitVoice: function (Hook) {
  74. if (this.m_GameEngine != null) return;
  75. this.m_Hook = Hook;
  76. this.m_GameEngine = this.m_Hook.m_GameClientEngine;
  77. this.statRecordDelayTime = 0;
  78. this.isStartRecord = false;
  79. this.touchTime = 0;
  80. if (!ThirdPartyVoiceInit()) this.node.active = false;
  81. },
  82. RecordStart: function () {
  83. if (this.m_nRecording.active) return;
  84. if (this.m_PlayingIndex != INVALD_CHAIR) return this.ShowTips('請等待語音播放結束後再進行錄音!');
  85. cc.audioEngine.pauseAll();
  86. this.ShowRecording(true);
  87. // this.m_RecordPath = ThirdPartyVoiceOnRecord(this);
  88. // if (!cc.sys.isBrowser) {
  89. // if (this.m_RecordPath == "") return this.ShowTips("没有录音权限!!!")
  90. // this.ShowRecording(true);
  91. // }
  92. ThirdPartyStartRecord();
  93. },
  94. PlayVoice: function (data) {
  95. if (this.m_Voices[data.dwSendUserID] == null) this.m_Voices[data.dwSendUserID] = '';
  96. this.m_Voices[data.dwSendUserID] += data.szVID;
  97. if (data.byPlatform == 0) {
  98. data.szVID = this.m_Voices[data.dwSendUserID];
  99. data.szVID = data.szVID.replace(/\n/g, '');
  100. this.m_Voices[data.dwSendUserID] = '';
  101. this.m_Sequence.push(data);
  102. this.receivCount++;
  103. console.log("声音传回完结0", this.receivCount)
  104. if (this.isPlaying) return;
  105. this.OnPlayFinish();
  106. } else {
  107. this.receivCount++;
  108. console.log("声音传回没完结1", this.receivCount)
  109. }
  110. },
  111. onRecordFinish: function (data) {
  112. if (data == '') {
  113. cc.audioEngine.resumeAll();
  114. this.ShowRecording(false);
  115. this.ShowTips('錄製時間過短!');
  116. return;
  117. }
  118. var str = data;
  119. while (str.length != 0) {
  120. if (str.length > 255) {
  121. this.m_GameEngine.OnSendUserVoice(2, str.slice(0, 255));
  122. str = str.slice(255);
  123. } else {
  124. this.m_GameEngine.OnSendUserVoice(0, str.slice(0));
  125. str = '';
  126. }
  127. }
  128. },
  129. RecordOver: function () {
  130. if (this.m_RecordStart == null) return
  131. this.m_RecordTime = new Date().getTime() - this.m_RecordStart;
  132. this.m_RecordStart = null;
  133. let str = ThirdPartyStopRecord((this.m_RecordTime < 1000 ? '0' : '1'));
  134. console.log("RecordOver--1", str.length)
  135. if (cc.sys.isNative) {
  136. if (cc.sys.OS_ANDROID == cc.sys.os) {
  137. if (str == '') {
  138. cc.audioEngine.resumeAll();
  139. this.ShowRecording(false);
  140. this.ShowTips('錄製時間過短!');
  141. return;
  142. }
  143. this.sendCount = 0;
  144. this.receivCount = 0;
  145. //todo
  146. //this.m_Voices[g_GlobalUserInfo.GetGlobalUserData().dwUserID] = '';
  147. while (str.length != 0) {
  148. if (str.length > 2559) {
  149. this.m_GameEngine.OnSendUserVoice_A(2, str.slice(0, 2559));
  150. str = str.slice(2559);
  151. this.sendCount++;
  152. } else {
  153. this.m_GameEngine.OnSendUserVoice_A(0, str.slice(0));
  154. str = '';
  155. this.sendCount++;
  156. console.log("发送声音count: ", this.sendCount)
  157. }
  158. // if (str.length > 255) {
  159. // this.m_GameEngine.OnSendUserVoice(2, str.slice(0, 255));
  160. // str = str.slice(255);
  161. // this.sendCount++;
  162. // } else {
  163. // this.m_GameEngine.OnSendUserVoice(0, str.slice(0));
  164. // str = '';
  165. // this.sendCount++;
  166. // console.log("发送声音count: ", this.sendCount)
  167. // }
  168. // if (str.length > 255) {
  169. // setTimeout((s) => {
  170. // this.m_GameEngine.OnSendUserVoice(2, s);
  171. // }, 40, str.slice(0, 255))
  172. // str = str.slice(255);
  173. // this.sendCount++;
  174. // } else {
  175. // setTimeout((s) => {
  176. // this.m_GameEngine.OnSendUserVoice(0, s);
  177. // }, 40, str.slice(0))
  178. // str = '';
  179. // this.sendCount++;
  180. // console.log("发送声音count: ", this.sendCount)
  181. // }
  182. }
  183. this.ShowRecording(false);
  184. } else {
  185. this.ShowRecording(false);
  186. }
  187. cc.audioEngine.resumeAll();
  188. }
  189. console.log("RecordOver--2")
  190. // ThirdPartyVoiceStopRecord(this);
  191. // ThirdPartyVoiceOnUpload(this, this.m_RecordPath);
  192. // if(cc.sys.isBrowser) {
  193. // var self = this;
  194. // var voice = {
  195. // localId: '',
  196. // serverId: '',
  197. // viewID: 65535,
  198. // };
  199. // wx.stopRecord({
  200. // success: function (res1) {
  201. // voice.localId = res1.localId;
  202. // wx.uploadVoice({
  203. // localId: voice.localId,
  204. // success: function (res2) {
  205. // voice.serverId = res2.serverId;
  206. // self.OnRecordOver(voice);
  207. // }
  208. // });
  209. // }
  210. // });
  211. // } else {
  212. // }
  213. },
  214. //发送完成回调
  215. OnRecordOver: function (RecordStr) {
  216. this.ShowRecording(false);
  217. if (cc.sys.isBrowser) {
  218. //添加无需加载队列
  219. RecordStr.viewID = GameDef.MYSELF_VIEW_ID;
  220. this.m_SelfArr.push(RecordStr);
  221. //发送语音通知
  222. this.m_GameEngine.OnSendUserVoice(1, RecordStr.serverId);
  223. } else {
  224. var RecordArr = RecordStr.split(',');
  225. //添加无需加载队列
  226. RecordArr[2] = GameDef.MYSELF_VIEW_ID;
  227. this.m_SelfArr.push(RecordArr);
  228. //发送语音通知
  229. this.m_GameEngine.OnSendUserVoice(0, RecordArr[0]);
  230. }
  231. this.CheckPlayArr();
  232. },
  233. OnLoadVoice: function (RecordStr) {
  234. if (cc.sys.isBrowser) {
  235. for (var i in this.m_LoadArr) {
  236. if (this.m_LoadArr[i].serverId == RecordStr.serverId) {
  237. this.m_LoadArr[i].localId = RecordStr.localId;
  238. this.m_WaitArr.push(this.m_LoadArr.splice(i, 1)[0]);
  239. break
  240. }
  241. }
  242. } else {
  243. var RecordArr = RecordStr.split(',');
  244. for (var i in this.m_LoadArr) {
  245. if (this.m_LoadArr[i][0] == RecordArr[0]) {
  246. this.m_LoadArr[i][1] = RecordArr[1];
  247. this.m_WaitArr.push(this.m_LoadArr.splice(i, 1)[0]);
  248. break
  249. }
  250. }
  251. }
  252. this.CheckPlayArr();
  253. },
  254. // OnPlayFinish: function () {
  255. // this.ShowPlaying(INVALD_CHAIR);
  256. // this.CheckPlayArr();
  257. // },
  258. OnPlayFinish: function () {
  259. if (this.m_Sequence.length > 0) {
  260. var data = this.m_Sequence.shift();
  261. if (!this.isPlaying) {
  262. this.isPlaying = true;
  263. cc.audioEngine.pauseAll();
  264. }
  265. var wChairID = this.m_GameEngine.GetUserChairID(data.dwSendUserID);
  266. var wViewID = this.m_GameEngine.SwitchViewChairID(wChairID);
  267. this.ShowPlaying(wViewID);
  268. ThirdPartyPlayVoice(data.szVID);
  269. } else {
  270. this.isPlaying = false;
  271. cc.audioEngine.resumeAll();
  272. this.ShowPlaying(INVALD_CHAIR);
  273. }
  274. },
  275. CheckPlayArr: function () {
  276. if (this.m_PlayingIndex != INVALD_CHAIR || this.m_nRecording.active) return;
  277. if (cc.sys.isBrowser) {
  278. if (this.m_WaitArr.length > 0) {
  279. var playInfo = (this.m_WaitArr.splice(0, 1)[0]);
  280. var self = this;
  281. //播放音频文件
  282. wx.playVoice({
  283. localId: playInfo.localId,
  284. success: function () {
  285. self.ShowPlaying(playInfo.viewID);
  286. }
  287. });
  288. wx.onVoicePlayEnd({
  289. complete: function (res) {
  290. self.OnPlayFinish();
  291. }
  292. });
  293. }
  294. } else {
  295. if (this.m_WaitArr.length > 0) {
  296. var playInfo = this.m_WaitArr.splice(0, 1)[0];
  297. //播放音频文件
  298. ThirdPartyPlayVoice(playInfo[1]);
  299. //显示播放标志
  300. this.ShowPlaying(playInfo[2]);
  301. }
  302. }
  303. },
  304. //显示播放标志
  305. ShowPlaying: function (ViewID) {
  306. this.m_PlayingIndex = ViewID;
  307. for (var i = 0; i < this.m_PlayingArr.length; i++) {
  308. this.m_PlayingArr[i].node.active = (i == ViewID);
  309. }
  310. if (ViewID != INVALD_CHAIR) {
  311. var scalex = this.m_Hook.m_UserVoiceArr[ViewID].x >= 200 ? -1 : 1;
  312. this.m_PlayingArr[ViewID].node.setPosition(this.m_Hook.m_UserVoiceArr[ViewID].x, this.m_Hook.m_UserVoiceArr[ViewID].y);
  313. this.m_PlayingArr[ViewID].node.setScale(scalex, 1);
  314. this.m_TempBGM = cc.gSoundRes.GetBGMVolume();
  315. window.SaveSetting(window.SetKey_Music, 0);
  316. } else {
  317. window.SaveSetting(window.SetKey_Music, this.m_TempBGM);
  318. }
  319. //音量调整
  320. cc.gSoundRes.UpdateVolume();
  321. },
  322. //显示录制
  323. ShowRecording: function (bShow) {
  324. this.m_nRecording.active = bShow;
  325. if (bShow) {
  326. //开始计时
  327. this.m_RecordStart = new Date().getTime();
  328. this.m_TempBGM = cc.gSoundRes.GetBGMVolume();
  329. window.SaveSetting(window.SetKey_Music, 0);
  330. } else {
  331. window.SaveSetting(window.SetKey_Music, this.m_TempBGM);
  332. }
  333. //音量调整
  334. cc.gSoundRes.UpdateVolume();
  335. },
  336. //播放消息处理
  337. AddVoice: function (pVoice) {
  338. if (pVoice.byPlatform == 1) { //WX
  339. for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
  340. if (this.m_Hook.m_pIClientUserItem[i] == null) continue;
  341. if (this.m_Hook.m_pIClientUserItem[i].GetUserID() == pVoice.dwSendUserID) {
  342. if (i == GameDef.MYSELF_VIEW_ID) {
  343. for (var j = 0; j < this.m_SelfArr.length; j++) {
  344. if (this.m_SelfArr[j].serverId == pVoice.szVID) {
  345. this.m_SelfArr[j].viewID = i;
  346. this.m_WaitArr.push(this.m_SelfArr.splice(j, 1)[0]);
  347. break;
  348. }
  349. }
  350. } else {
  351. this.m_LoadArr.push({
  352. serverId: pVoice.szVID,
  353. localId: '',
  354. viewID: i
  355. });
  356. var self = this;
  357. wx.downloadVoice({
  358. serverId: pVoice.szVID,
  359. success: function (res) {
  360. self.OnLoadVoice({
  361. serverId: pVoice.szVID,
  362. localId: res.localId,
  363. viewID: i
  364. });
  365. }
  366. });
  367. }
  368. break;
  369. }
  370. }
  371. } else {
  372. for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
  373. if (this.m_Hook.m_pIClientUserItem[i] == null) continue;
  374. if (this.m_Hook.m_pIClientUserItem[i].GetUserID() == pVoice.dwSendUserID) {
  375. if (i == GameDef.MYSELF_VIEW_ID) { //本地消息
  376. for (var j = 0; j < this.m_SelfArr.length; j++) {
  377. if (this.m_SelfArr[j][0] == pVoice.szVID) {
  378. this.m_WaitArr.push(this.m_SelfArr.splice(j, 1)[0]);
  379. break;
  380. }
  381. }
  382. } else { //网络消息加载
  383. this.m_LoadArr.push([pVoice.szVID, '', i]);
  384. //下载音频文件
  385. ThirdPartyGVoiceLoadVoice(pVoice.szVID);
  386. }
  387. break;
  388. }
  389. }
  390. }
  391. this.CheckPlayArr();
  392. },
  393. update: function (dt) {
  394. this.statRecordDelayTime -= dt;
  395. if (this.statRecordDelayTime < 0) {
  396. this.statRecordDelayTime = 0;
  397. }
  398. if (this.isTouching) {
  399. this.touchTime -= dt;
  400. if (this.touchTime < 0) {
  401. this.touchTime = 0;
  402. if (this.isStartRecord == false) {
  403. this.RecordStart();
  404. this.isStartRecord = true;
  405. this.recordTime = 20;
  406. console.log("录音开始");
  407. }
  408. }
  409. }
  410. if (this.isStartRecord) {
  411. this.recordTime -= dt;
  412. if (this.recordTime < 0 && this.isTouching) {
  413. this.RecordOver();
  414. this.isStartRecord = false;
  415. this.isTouching = false;
  416. console.log("录音");
  417. }
  418. }
  419. if (this.m_nRecording == null) return
  420. //录音动画
  421. if (this.m_nRecording.active) {
  422. var Now = new Date().getTime();
  423. if (this.m_RecordStart != null && Now - this.m_RecordStart > 59000) {
  424. this.RecordOver();
  425. this.ShowTips('錄音時長達到上限!');
  426. }
  427. }
  428. },
  429. });