AudioRecord.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. let topTipMsg = require("topTipMsg");
  2. let Global = require("Global");
  3. class AudioRecord {
  4. constructor() {
  5. if (CC_JSB) {
  6. this.phonePath = jsb.fileUtils.getWritablePath();
  7. } else {
  8. this.phonePath = "";
  9. }
  10. this.playerRecoeding = {};
  11. this.jsbPath = "org/cocos2dx/javascript/AudioRecord";
  12. this.initJSBFunc({});
  13. }
  14. refreshData() {
  15. this.callBackData = null;
  16. this.playerRecoeding = {};
  17. }
  18. initJSBFunc(data) {
  19. let callBackData = data;
  20. this.playerAudioingSprite = data.playerMap;
  21. window.AndroidAudioJSB = {};
  22. window.AndroidAudioJSB.onStartPlayer = (index) => {
  23. callBackData && callBackData.onStartPlayer && callBackData.onStartPlayer();
  24. }
  25. window.AndroidAudioJSB.onFinishPlayer = (index) => {
  26. this.delteDataByMap(index);
  27. this.refreshUI();
  28. callBackData && callBackData.onFinishPlayer && callBackData.onFinishPlayer();
  29. }
  30. window.AndroidAudioJSB.onStopPlayer = () => {
  31. callBackData && callBackData.onStopPlayer && callBackData.onStopPlayer();
  32. }
  33. window.AndroidAudioJSB.onFailurePlayer = () => {
  34. callBackData && callBackData.onFailurePlayer && callBackData.onFailurePlayer();
  35. }
  36. window.AndroidAudioJSB.onStartRecorder = () => {
  37. callBackData && callBackData.onStartRecorder && callBackData.onStartRecorder();
  38. }
  39. window.AndroidAudioJSB.onCanceRecorderl = () => {
  40. callBackData && callBackData.onCanceRecorderl && callBackData.onCanceRecorderl();
  41. }
  42. window.AndroidAudioJSB.onStopRecorder = () => {
  43. callBackData && callBackData.onStopRecorder && callBackData.onStopRecorder();
  44. }
  45. window.AndroidAudioJSB.onFailureRecorder = () => {
  46. callBackData && callBackData.onFailureRecorder && callBackData.onFailureRecorder();
  47. }
  48. }
  49. getAudioData(chairID) {
  50. let fileName = this.fileName + "";
  51. let fileData = jsb.fileUtils.getDataFromFile(jsb.fileUtils.getWritablePath() + "GameAudio/" + fileName);
  52. let arrayBuffer = new ArrayBuffer(8 + 1 + fileName.length + fileData.length);
  53. let dv = new DataView(arrayBuffer);
  54. dv.pos = 8;
  55. Global.SetUint8(dv, chairID);
  56. for (let j = 0; j < fileName.length; j += 1) {
  57. Global.SetUint8(dv, fileName[j]);
  58. }
  59. for (let i = 0; i < fileData.length; i += 1) {
  60. Global.SetUint8(dv, fileData[i]);
  61. }
  62. return dv;
  63. }
  64. saveAudioPlay(data, TchairID) {
  65. let dataList = {};
  66. let index = 0;
  67. let dv = new DataView(data.slice(8));
  68. dv.pos = 0;
  69. dataList.chairID = Global.GetUint8(dv);
  70. dataList.fileName = "";
  71. dataList.datas = [];
  72. for (let i = 0; i < 13; i += 1) {
  73. dataList.fileName += Global.GetUint8(dv);
  74. }
  75. if (dataList.chairID == TchairID) {
  76. index = this.startPlayer(dataList.fileName);
  77. } else {
  78. for (let i = 0; i < dv.byteLength - 13 - 1; i += 1) {
  79. dataList.datas.push(Global.GetUint8(dv));
  80. }
  81. dataList.fileDatas = new Uint8Array(dataList.datas);
  82. let dirPath = jsb.fileUtils.getWritablePath() + "GameAudio/";
  83. let filepath = dirPath + dataList.fileName;
  84. if (!jsb.fileUtils.isDirectoryExist(dirPath)) {
  85. jsb.fileUtils.createDirectory(dirpath);
  86. } if (jsb.fileUtils.writeDataToFile(dataList.fileDatas, filepath)) {
  87. index = this.startPlayer(dataList.fileName);
  88. }
  89. }
  90. this.addDataToMap(TchairID, index);
  91. this.refreshUI();
  92. }
  93. addDataToMap(TchairID, index) {
  94. if (!this.playerRecoeding[TchairID]) {
  95. this.playerRecoeding[TchairID] = [];
  96. }
  97. this.playerRecoeding[TchairID].push(index);
  98. this.callBackData && this.callBackData.onStartPlayer(TchairID);
  99. }
  100. delteDataByMap(index) {
  101. let keys = Object.keys(this.playerRecoeding);
  102. for (let i = 0; i < keys.length; i += 1) {
  103. let chairData = keys[i];
  104. if (this.playerRecoeding[chairData].indexOf(index) >= 0) {
  105. this.playerRecoeding[chairData].remove(index);
  106. if (this.playerRecoeding[chairData].length == 0) {
  107. delete this.playerRecoeding[chairData];
  108. }
  109. }
  110. }
  111. }
  112. getSpeakState() {
  113. let keys = Object.keys(this.playerRecoeding);
  114. let map = [];
  115. for (let i = 0; i < keys.length; i += 1) {
  116. let chairData = keys[i];
  117. if (chairData.length > 0) {
  118. map.push(chairData);
  119. }
  120. }
  121. return map;
  122. }
  123. refreshUI() {
  124. let map = this.getSpeakState();
  125. for (let i in this.playerAudioingSprite) {
  126. this.playerAudioingSprite[i].active = false;
  127. }
  128. for (let i = 0; i < map.length; i += 1) {
  129. let chairID = map[i];
  130. this.playerAudioingSprite[chairID].active = true;
  131. }
  132. }
  133. //开始录音
  134. startRecorder(fileName) {
  135. CC_JSB && jsb.reflection.callStaticMethod(
  136. this.jsbPath,
  137. "onStartRecorder",
  138. "(Ljava/lang/String;)V",
  139. fileName
  140. )
  141. }
  142. //取消录音
  143. cancelRecorder(fileName) {
  144. CC_JSB && jsb.reflection.callStaticMethod(
  145. this.jsbPath,
  146. "onCancelRecorder",
  147. "(Ljava/lang/String;)V",
  148. fileName
  149. )
  150. }
  151. //结束录音
  152. stopRecorder(fileName) {
  153. CC_JSB && jsb.reflection.callStaticMethod(
  154. this.jsbPath,
  155. "onStopRecorder",
  156. "(Ljava/lang/String;)V",
  157. fileName
  158. )
  159. }
  160. //开始播放录音
  161. startPlayer(fileName) {
  162. return CC_JSB && jsb.reflection.callStaticMethod(
  163. this.jsbPath,
  164. "onStartPlayer",
  165. "(Ljava/lang/String;)I",
  166. fileName
  167. )
  168. }
  169. //结束播放录音
  170. stopPlayer() {
  171. CC_JSB && jsb.reflection.callStaticMethod(
  172. this.jsbPath,
  173. "onStopPlayer",
  174. "()V"
  175. )
  176. }
  177. //删除录音缓存
  178. deleteRecorder() {
  179. CC_JSB && jsb.reflection.callStaticMethod(
  180. this.jsbPath,
  181. "onDeleteRecorder",
  182. "()Z"
  183. )
  184. }
  185. //麦克风音量
  186. backMicNumber() {
  187. return CC_JSB && jsb.reflection.callStaticMethod(
  188. this.jsbPath,
  189. "backMicNumber",
  190. "()I"
  191. )
  192. }
  193. clickRecorder(data) {
  194. let succCallback = data.succ;
  195. let failCallback = data.fail;
  196. let micCallback = data.mic;
  197. let sprite = data.sprite;
  198. this.fileName = Date.now();
  199. this.recording = true;
  200. this.stopRecording = false;
  201. this.time = 0;
  202. this.startRecorder(this.fileName);
  203. let event_underTime = () => {
  204. clearInterval(intervalID);
  205. this.cancelRecorder(this.fileName);
  206. }
  207. let event_timeEnd = () => {
  208. clearInterval(intervalID);
  209. this.stopRecorder(this.fileName);
  210. }
  211. let event_cancel = () => {
  212. clearInterval(intervalID);
  213. this.cancelRecorder(this.fileName);
  214. }
  215. let intervalID = setInterval(() => {
  216. this.time += 25;
  217. if (this.stopRecording == true) {
  218. event_cancel();
  219. } else if (this.recording == false && this.time < 1000) {
  220. event_underTime();
  221. if (failCallback) {
  222. failCallback();
  223. } else {
  224. topTipMsg.showTopTipMsg("说话时间太短了,翻译");
  225. }
  226. } else if (this.recording == false) {
  227. event_timeEnd();
  228. succCallback();
  229. } else if (this.time >= 10000) {
  230. event_timeEnd();
  231. succCallback();
  232. }
  233. let mic = this.backMicNumber();
  234. micCallback(mic);
  235. sprite.fillRange = (9000 - this.time) / 9000;
  236. }, 25);
  237. }
  238. overRecorder() {
  239. this.recording = false;
  240. }
  241. removeRecorder() {
  242. this.stopRecording = true;
  243. }
  244. stopAndDeleteRecorder() {
  245. this.stopPlayer();
  246. this.deleteRecorder();
  247. }
  248. static getInstance() {
  249. if (!AudioRecord.instance) {
  250. AudioRecord.instance = new AudioRecord();
  251. window.AudioRecord = AudioRecord.instance;
  252. }
  253. return AudioRecord.instance;
  254. }
  255. }
  256. module.exports = AudioRecord.getInstance();