You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.2 KiB

  1. local PluginBase = require("luaScript.Plugins.PluginBase")
  2. local PluginAndroidGVoice = class("PluginAndroidGVoice" , PluginBase)
  3. function PluginAndroidGVoice:ctor()
  4. PluginAndroidGVoice.super.ctor(self);
  5. self.PluginGVoice = nil
  6. end
  7. -- 启动插件
  8. function PluginAndroidGVoice:start(voiceType)
  9. log("PluginAndroidGVoice::start()")
  10. local pathRecording = cc.FileUtils:getInstance():getWritablePath().."recording.dat";
  11. local pathDownload = cc.FileUtils:getInstance():getWritablePath().."download.dat";
  12. -- self.PluginGVoice = cc.PluginManager:getInstance():createPlugin("PluginGVoice" , "com/ddgame/plugin/PluginGVoice");
  13. if isAndroidPlatform() then
  14. self.PluginGVoice = cc.PluginManager:getInstance():createPlugin("PluginGVoice" , "com/ddgame/plugin/PluginGVoice");
  15. elseif isIOSPlatform() then
  16. -- self.plugin = cc.PluginManager:getInstance():createPlugin("PluginGVoice" , "PluginGVoice");
  17. end
  18. if self.PluginGVoice then
  19. -- log("PluginAndroidGVoice::start() uid = "..table.tostring(app.user.loginInfo))
  20. local developerInfo =
  21. {
  22. -- gameId = "1574117261",
  23. gameId = "478434646",
  24. userId = app.user.loginInfo.uid,
  25. gameKey = "5b7aab45c3f56d2816fa48a8273dae3c",
  26. callback = handler(self, self.onResult), -- 回调函数
  27. pathRecording = pathRecording,
  28. pathDownload = pathDownload,
  29. -- voiceType = voiceType,
  30. };
  31. log("PluginAndroidGVoice::start() developerInfo = ", table.tostring(developerInfo))
  32. self.PluginGVoice:callVoid("initPlugin", developerInfo);
  33. else
  34. log("PluginAndroidGVoice::start() failed")
  35. end
  36. end
  37. -- 停止插件
  38. function PluginAndroidGVoice:stop()
  39. if self.PluginGVoice then
  40. cc.PluginManager:getInstance():removePlugin(self.PluginGVoice);
  41. self.PluginGVoice = nil;
  42. end
  43. end
  44. -- 开始录音
  45. function PluginAndroidGVoice:ApplyMessageKey()
  46. if self.PluginGVoice then
  47. self.PluginGVoice:callVoid("ApplyMessageKey");
  48. end
  49. end
  50. -- 开始录音
  51. function PluginAndroidGVoice:startRecord()
  52. if self.PluginGVoice then
  53. self.PluginGVoice:callVoid("startRecord");
  54. end
  55. end
  56. -- 取消录音
  57. function PluginAndroidGVoice:cancelRecord()
  58. if self.PluginGVoice then
  59. self.PluginGVoice:callVoid("cancelRecord");
  60. end
  61. end
  62. -- 结束录音
  63. function PluginAndroidGVoice:stopRecord()
  64. log("PluginAndroidGVoice:stopRecord()");
  65. if self.PluginGVoice then
  66. self.PluginGVoice:callVoid("stopRecord");
  67. end
  68. end
  69. -- 播放录音
  70. function PluginAndroidGVoice:playRecord(filePath)
  71. log("PluginAndroidGVoice:playRecord() fileId = ", filePath);
  72. -- 记录回调
  73. if self.PluginGVoice then
  74. local data =
  75. {
  76. filePath = tostring(filePath)
  77. }
  78. log("PluginAndroidGVoice:playRecord() data = ", table.tostring(data))
  79. self.PluginGVoice:callVoid("playRecord", data);
  80. end
  81. end
  82. -- 停止播放录音
  83. function PluginAndroidGVoice:stopPlayRecord()
  84. log("PluginAndroidGVoice:stopPlayRecord() ");
  85. if self.PluginGVoice then
  86. self.PluginGVoice:callVoid("stopPlayRecord");
  87. end
  88. end
  89. -- 下载录音
  90. -- function PluginAndroidGVoice:downGloadRecord(nfileID, nfileID, filePath, downloadCallback)
  91. function PluginAndroidGVoice:downloadRecord(nUserId, nfileID, downloadCallback)
  92. self.downloadCallback = downloadCallback
  93. if self.PluginGVoice then
  94. local data =
  95. {
  96. recordFileID = tostring(nfileID),
  97. recordTag = tostring(nUserId),
  98. -- filePath = tostring(filePath),
  99. play = true,
  100. }
  101. log("PluginAndroidGVoice:downloadRecord() data = ", table.tostring(data))
  102. self.PluginGVoice:callVoid("downloadRecord", data);
  103. end
  104. end
  105. -- 加入房间
  106. function PluginAndroidGVoice:JoinRoom(room)
  107. if self.PluginGVoice then
  108. local data =
  109. {
  110. room = tostring(room)
  111. }
  112. log("PluginAndroidGVoice:JoinRoom() data = ", table.tostring(data))
  113. self.PluginGVoice:callVoid("JoinRoom", data);
  114. end
  115. end
  116. -- 退出房间
  117. function PluginAndroidGVoice:QuitRoom(room)
  118. if self.PluginGVoice then
  119. local data =
  120. {
  121. room = tostring(room)
  122. }
  123. log("PluginAndroidGVoice:QuitRoom() data = ", table.tostring(data))
  124. self.PluginGVoice:callVoid("QuitRoom", data);
  125. end
  126. end
  127. -- 开启麦克风
  128. function PluginAndroidGVoice:OpenMic()
  129. log("PluginAndroidGVoice:OpenMic() ");
  130. if self.PluginGVoice then
  131. self.PluginGVoice:callVoid("OpenMic");
  132. end
  133. end
  134. -- 关闭麦克风
  135. function PluginAndroidGVoice:CloseMic()
  136. log("PluginAndroidGVoice:CloseMic() ");
  137. if self.PluginGVoice then
  138. self.PluginGVoice:callVoid("CloseMic");
  139. end
  140. end
  141. -- 开启扬声器
  142. function PluginAndroidGVoice:OpenSpeaker()
  143. log("PluginAndroidGVoice:OpenSpeaker() ");
  144. if self.PluginGVoice then
  145. self.PluginGVoice:callVoid("OpenSpeaker");
  146. end
  147. end
  148. -- 关闭扬声器
  149. function PluginAndroidGVoice:CloseSpeaker()
  150. log("PluginAndroidGVoice:CloseSpeaker() ");
  151. if self.PluginGVoice then
  152. self.PluginGVoice:callVoid("CloseSpeaker");
  153. end
  154. end
  155. --
  156. function PluginAndroidGVoice:OnApplicationPause()
  157. log("PluginAndroidGVoice:OnApplicationPause() ");
  158. if self.PluginGVoice then
  159. self.PluginGVoice:callVoid("OnApplicationPause");
  160. end
  161. end
  162. --
  163. function PluginAndroidGVoice:Update()
  164. -- log("PluginAndroidGVoice:Update() ");
  165. if self.PluginGVoice then
  166. self.PluginGVoice:callVoid("Update");
  167. end
  168. end
  169. -- 回调函数
  170. function PluginAndroidGVoice:onResult(result)
  171. log("PluginAndroidGVoice:onResult() result = ", result)
  172. local resultJson = json.decode(result);
  173. log("PluginAndroidGVoice:onResult() resultJson = ", table.tostring(resultJson))
  174. local code = resultJson.code;
  175. local msg = resultJson.message;
  176. -- if code == 7001 then -- 初始化成功
  177. if code == 1001 then -- 初始化成功
  178. --showTooltip("呀呀语音 初始化成功")
  179. local info = json.decode(msg)
  180. print("初始化成功", "info = ", table.tostring(info))
  181. -- elseif code == 7002 then -- 初始化失败
  182. elseif code == 1002 then -- 初始化失败
  183. --showTooltip("呀呀语音 初始化失败")
  184. local info = json.decode(msg)
  185. print("初始化失败", "info = ", table.tostring(info))
  186. -- elseif code == 7003 then -- 录音成功
  187. elseif code == 7001 then -- 录音成功
  188. --showTooltip("呀呀语音 录音成功")
  189. local info = json.decode(msg)
  190. print("录音成功", "info = ", table.tostring(info))
  191. app:dispatchEvent({name = "recordGCallback", fileId = info.fileId, filePath = info.filePath, recordTime = info.recordTime });
  192. -- elseif code == 7004 then -- 录音失败
  193. elseif code == 7002 then -- 录音失败
  194. --showTooltip("呀呀语音 录音失败")
  195. local info = json.decode(msg)
  196. print("录音失败", "info = ", table.tostring(info))
  197. -- elseif code == 7005 then -- 录音下载成功
  198. elseif code == 7003 then -- 录音下载成功
  199. --showTooltip("呀呀语音 录音下载成功")
  200. local info = json.decode(msg)
  201. print("录音下载成功", "info = ", table.tostring(info))
  202. if self.downloadCallback then
  203. -- self.downloadCallback(info.recordTag, info.filePath);
  204. self.downloadCallback( info.filePath);
  205. end
  206. -- elseif code == 7006 then -- 录音下载失败
  207. elseif code == 7004 then -- 录音下载失败
  208. --showTooltip("呀呀语音 录音下载失败")
  209. local info = json.decode(msg)
  210. print("录音下载失败", "info = ", table.tostring(info))
  211. elseif code == 7011 then -- 加入房间
  212. local info = json.decode(msg)
  213. print("加入房间", "info = ", table.tostring(info))
  214. -- self:OpenMic()
  215. self:OpenSpeaker()
  216. end
  217. end
  218. return PluginAndroidGVoice