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.

267 lines
6.8 KiB

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