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.

476 lines
13 KiB

  1. -- 聊天组件 配合RoomChatView使用
  2. local ChatDefine = require("luaScript.GameChatDefine")
  3. local RoomVoiceComponentView = class("RoomVoiceComponentView", cc.UIView)
  4. function RoomVoiceComponentView:ctor(btnVoice)
  5. RoomVoiceComponentView.super.ctor(self)
  6. self:loadUI()
  7. self.btnVoice = btnVoice
  8. self.playerVoice = {}
  9. -- 我的视图椅子号
  10. self.myViewId = app.room:getViewIdByUserId(app.user.loginInfo.uid) or 1
  11. --系统的音效状态
  12. self.soundState = app.systemSetting.info.sound
  13. self.recordingTime = os.time()
  14. end
  15. function RoomVoiceComponentView:loadUI()
  16. local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_voice.ui")
  17. self.ui = ui
  18. self:addChild(ui)
  19. end
  20. function RoomVoiceComponentView:onEnter()
  21. RoomVoiceComponentView.super.onEnter(self)
  22. self:initBindEvent()
  23. --初始voice按钮事件
  24. if self.btnVoice and not tolua.isnull(self.btnVoice) then
  25. -- self.btnVoice:registerClick(handler(self , self.stopRecord), handler(self , self.beginRecord), handler(self , self.stopRecord))
  26. self.btnVoice:registerClick(handler(self , self.stopGRecord), handler(self , self.beginGRecord), handler(self , self.stopGRecord))
  27. end
  28. --默认隐藏
  29. self:hideSpeaking()
  30. end
  31. -- 显示屏幕中间滑动手指取消录音的提示
  32. function RoomVoiceComponentView:showSpeaking()
  33. logD("RoomVoiceComponentView:showSpeaking")
  34. self.ui.Items.Layout_luyinzhong:setVisible(true)
  35. self.ui.Items.Text_Tips:setText("")
  36. if isAndroidPlatform() then
  37. self.ui.Items.Layout_luyinzhong:playClip("speaking")
  38. elseif isIOSPlatform() then
  39. self.ui.Items.Text_Tips:setText("正在开启录音设备")
  40. self.ui.Items.Layout_3:setVisible(false);
  41. runDelay(1.1, function ()
  42. logD("RoomVoiceComponentView:runDelay")
  43. self.ui.Items.Text_Tips:setText("")
  44. self.ui.Items.Layout_3:setVisible(true);
  45. self.ui.Items.Layout_luyinzhong:playClip("speaking")
  46. end)
  47. end
  48. end
  49. -- 隐藏屏幕中间滑动手指取消录音的提示
  50. function RoomVoiceComponentView:hideSpeaking()
  51. self.ui.Items.Layout_luyinzhong:setVisible(false)
  52. self.ui.Items.Layout_luyinzhong:stopClip("speaking")
  53. end
  54. -- 开始录音
  55. function RoomVoiceComponentView:beginRecord()
  56. local t = os.time() - self.recordingTime
  57. if not app.user:canSendChatMessage(2) or t < ChatDefine.TimeInterval[2] then
  58. showTooltip("您发送语音信息的间隔时间太短,请稍后再发!")
  59. return
  60. end
  61. if self.recording then
  62. return
  63. end
  64. -- 停止所有的表情声音
  65. -- if self.lastSound then
  66. -- stopVoice(self.lastSound)
  67. -- end
  68. --为了不影响录制的过程,还可以听到音效,关闭所有的音效
  69. app.systemSetting.info.sound = false
  70. --暂停音乐
  71. cc.AudioController:getInstance():pause();
  72. --暂停音效
  73. --cc.AudioController:getInstance():setPlaySound(false)
  74. -- 停止所有的玩家语音
  75. app.plugin:stopPlayRecord()
  76. -- 如果有玩家正在说话
  77. if self.lastSpeakingSeatId then
  78. app.user:dispatchEvent({name = GAME_EVENT.CHAT_STOP_VOICE , viewId = self.lastSpeakingSeatId})
  79. -- self:shopVoice(self.lastSpeakingSeatId)
  80. end
  81. self.recording = true;
  82. self.recordingTime = os.time()
  83. --showTooltip("开始录音")
  84. self:showSpeaking()
  85. app.plugin:startRecord()
  86. end
  87. -- 开始录音,腾讯语音GVoice
  88. function RoomVoiceComponentView:beginGRecord()
  89. local t = os.time() - self.recordingTime
  90. if t < ChatDefine.TimeInterval[2] then
  91. showTooltip("您发送语音信息的间隔时间太短,请稍后再发!")
  92. return
  93. end
  94. if self.recording then
  95. return
  96. end
  97. --为了不影响录制的过程,还可以听到音效,关闭所有的音效
  98. app.systemSetting.info.sound = false
  99. --暂停音乐
  100. cc.AudioController:getInstance():pause();
  101. -- 停止所有的玩家语音
  102. app.plugin:stopGPlayRecord()
  103. -- 如果有玩家正在说话, todol 下面这个以后再改
  104. if self.lastSpeakingSeatId then
  105. app.user:dispatchEvent({name = GAME_EVENT.CHAT_STOP_VOICE , viewId = self.lastSpeakingSeatId})
  106. end
  107. self.recording = true;
  108. self.recordingTime = os.time()
  109. self:showSpeaking()
  110. app.plugin:startGRecord()
  111. end
  112. -- 结束录音
  113. function RoomVoiceComponentView:stopRecord()
  114. if not self.recording then
  115. return
  116. end
  117. -- showTooltip("结束录音111")
  118. self.recording = false;
  119. self:hideSpeaking();
  120. app.systemSetting.info.sound = self.soundState
  121. --继续音乐
  122. cc.AudioController:getInstance():resume();
  123. --恢复音效
  124. --cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound)
  125. if cc.Application:getInstance():getTargetPlatform() == 0 then
  126. local jsonData =
  127. {
  128. recordUrl = "recordUrl",
  129. recordTime = 2,
  130. }
  131. local content = json.encode(jsonData)
  132. if app.user:sendChatMessage(2, content) then
  133. -- 记录我发送过的语音
  134. self.playerVoice[self.myViewId] = {}
  135. self.playerVoice[self.myViewId].filePath = ""
  136. self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl
  137. self.playerVoice[self.myViewId].recordTime = jsonData.recordTime;
  138. -- 播放我的语音
  139. self:_replayRecord(self.myViewId)
  140. end
  141. return
  142. end
  143. app.plugin:stopRecord()
  144. end
  145. -- 结束录音,腾讯语音GVoice
  146. function RoomVoiceComponentView:stopGRecord()
  147. if not self.recording then
  148. return
  149. end
  150. self.recording = false;
  151. self:hideSpeaking();
  152. app.systemSetting.info.sound = self.soundState
  153. --继续音乐
  154. cc.AudioController:getInstance():resume();
  155. if cc.Application:getInstance():getTargetPlatform() == 0 then
  156. local jsonData =
  157. {
  158. -- recordUrl = "recordUrl",
  159. recordUrl = "fileId",
  160. recordTime = 2,
  161. }
  162. local content = json.encode(jsonData)
  163. if app.user:sendChatMessage(2, content) then
  164. -- 记录我发送过的语音
  165. self.playerVoice[self.myViewId] = {}
  166. self.playerVoice[self.myViewId].filePath = ""
  167. self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl
  168. self.playerVoice[self.myViewId].recordTime = jsonData.recordTime;
  169. -- 播放我的语音
  170. self:_replayGRecord(self.myViewId)
  171. end
  172. return
  173. end
  174. app.plugin:stopGRecord()
  175. end
  176. -- 取消录音
  177. function RoomVoiceComponentView:cancelRecord()
  178. if not self.recording then
  179. return
  180. end
  181. --继续音乐
  182. cc.AudioController:getInstance():resume();
  183. --恢复音效
  184. --cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound)
  185. app.systemSetting.info.sound = self.soundState
  186. --showTooltip("取消录音")
  187. self.recording = false;
  188. self:hideSpeaking();
  189. app.plugin:cancelRecord()
  190. end
  191. -- 播放回调函数
  192. function RoomVoiceComponentView:recordCallback(event)
  193. --恢复音乐
  194. if app.systemSetting.info.music then
  195. cc.AudioController:getInstance():resume();
  196. end
  197. if not event then return end
  198. local filePath, recordUrl, recordTime = event.filePath, event.recordUrl, event.recordTime
  199. log("log_event", filePath, recordUrl, recordTime)
  200. if not filePath or not recordUrl or not recordTime then
  201. return
  202. end
  203. recordTime = math.ceil(recordTime / 1000);
  204. --腾讯语音GVoice 开启录音设备大概有1秒延迟
  205. if isIOSPlatform() then
  206. recordTime = recordTime - 1;
  207. end
  208. local jsonData =
  209. {
  210. recordUrl = recordUrl,
  211. recordTime = recordTime,
  212. }
  213. if recordTime<=1 then
  214. showTooltip("录音时间过短!")
  215. return
  216. end
  217. local config=getSubGameConfig(tonumber(app.gameId))
  218. if config and config.belongType==2 then
  219. local nMyUserId = app.user.loginInfo.uid
  220. self.myViewId = app.room:getViewIdByUserId(nMyUserId)
  221. end
  222. local content = json.encode(jsonData);
  223. if app.user:sendChatMessage(2, content) then
  224. -- 记录我发送过的语音
  225. self.playerVoice[self.myViewId] = {}
  226. self.playerVoice[self.myViewId].filePath = filePath;
  227. self.playerVoice[self.myViewId].recordUrl = recordUrl;
  228. self.playerVoice[self.myViewId].recordTime = recordTime;
  229. -- 播放我的语音
  230. self:_replayRecord(self.myViewId)
  231. end
  232. end
  233. -- 播放回调函数
  234. function RoomVoiceComponentView:recordGCallback(event)
  235. --恢复音乐
  236. if app.systemSetting.info.music then
  237. cc.AudioController:getInstance():resume();
  238. end
  239. if not event then return end
  240. local fileId, filePath, recordTime = event.fileId, event.filePath, event.recordTime
  241. log("log_event", fileId, filePath, recordTime)
  242. if not fileId or not filePath or not recordTime then
  243. return
  244. end
  245. recordTime = math.ceil(recordTime / 1);
  246. local jsonData =
  247. {
  248. recordUrl = fileId,
  249. recordTime = recordTime,
  250. }
  251. if recordTime<=1 then
  252. showTooltip("录音时间过短!")
  253. return
  254. end
  255. local config=getSubGameConfig(tonumber(app.gameId))
  256. if config and config.belongType==2 then
  257. local nMyUserId = app.user.loginInfo.uid
  258. self.myViewId = app.room:getViewIdByUserId(nMyUserId)
  259. end
  260. local content = json.encode(jsonData);
  261. if app.user:sendChatMessage(2, content) then
  262. -- 记录我发送过的语音
  263. self.playerVoice[self.myViewId] = {}
  264. self.playerVoice[self.myViewId].filePath = filePath;
  265. self.playerVoice[self.myViewId].recordUrl = fileId;
  266. self.playerVoice[self.myViewId].recordTime = recordTime;
  267. -- 播放我的语音
  268. self:_replayGRecord(self.myViewId)
  269. end
  270. end
  271. -- 重新播放某个玩家之前说的话
  272. function RoomVoiceComponentView:_replayRecord(nSeatShowId)
  273. log("RoomVoiceComponentView:_replayRecord() nSeatShowId = ", nSeatShowId);
  274. log("RoomVoiceComponentView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice));
  275. local data = self.playerVoice[nSeatShowId]
  276. if data then
  277. if data.filePath then
  278. app.plugin:playRecord(data.filePath)
  279. end
  280. local recordTime = data.recordTime
  281. self.lastSpeakingSeatId = nSeatShowId
  282. app.user:dispatchEvent({name = GAME_EVENT.CHAT_PLAY_VOICE , viewId = nSeatShowId,recordTime = recordTime})
  283. -- self:playVoice(nSeatShowId, recordTime, function()
  284. -- self.lastSpeakingSeatId = nil
  285. -- end)
  286. else
  287. showTooltip("无历史消息")
  288. end
  289. end
  290. -- 重新播放某个玩家之前说的话,Gvoice
  291. function RoomVoiceComponentView:_replayGRecord(nSeatShowId)
  292. log("RoomVoiceComponentView:_replayRecord() nSeatShowId = ", nSeatShowId);
  293. log("RoomVoiceComponentView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice));
  294. local data = self.playerVoice[nSeatShowId]
  295. if data then
  296. if data.filePath then
  297. app.plugin:playGRecord(data.filePath)
  298. end
  299. local recordTime = data.recordTime
  300. self.lastSpeakingSeatId = nSeatShowId
  301. app.user:dispatchEvent({name = GAME_EVENT.CHAT_PLAY_VOICE , viewId = nSeatShowId,recordTime = recordTime})
  302. -- self:playVoice(nSeatShowId, recordTime, function()
  303. -- self.lastSpeakingSeatId = nil
  304. -- end)
  305. else
  306. showTooltip("无历史消息")
  307. end
  308. end
  309. function RoomVoiceComponentView:initBindEvent()
  310. -- 录制完成回调
  311. self:bindEvent(app , "recordCallback" , handler(self , self.recordCallback))
  312. -- 录制完成回调,Gvoice
  313. self:bindEvent(app , "recordGCallback" , handler(self , self.recordGCallback))
  314. --收到语音消息
  315. self:bindEvent(app.user, GAME_EVENT.CHAT_VOICE_RESP, handler(self, self.onVoiceEvent))
  316. --收到语音消息,Gvoice
  317. self:bindEvent(app.user, GAME_EVENT.CHAT_VOICE_RESP, handler(self, self.onGVoiceEvent))
  318. self:bindEvent(app.user, GAME_EVENT.CHAT_REPLAY_VOICE, handler(self, self.onReplayVoiceEvent))
  319. --绑定change音效
  320. self:bindEvent(app , "onChangeYinXiao" , handler(self , self.onChangeYinXiao));
  321. end
  322. function RoomVoiceComponentView:onChangeYinXiao(data)
  323. self.soundState = app.systemSetting.info.sound
  324. end
  325. function RoomVoiceComponentView:onVoiceEvent(event)
  326. local nSeatShowId = event.viewId
  327. local nUserId = event.nUserId
  328. local recordUrl = event.recordUrl
  329. local recordTime = event.recordTime
  330. -- 下载玩家的语音文件并播放
  331. -- 下载完成后返回该文件在本地存储的路径
  332. app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath)
  333. local userId = tonumber(recordTag)
  334. local seatShowId = app.room:getViewIdByUserId(userId)
  335. if not seatShowId or not self.playerVoice[seatShowId] then
  336. -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId);
  337. return
  338. end
  339. self.playerVoice[seatShowId].filePath = filePath
  340. end);
  341. -- 记录该玩家的语音文件,重播要用
  342. self.playerVoice[nSeatShowId] = {}
  343. self.playerVoice[nSeatShowId].recordUrl = recordUrl;
  344. self.playerVoice[nSeatShowId].recordTime = recordTime;
  345. -- 该玩家头像上播放说话的效果
  346. self:_replayRecord(nSeatShowId)
  347. end
  348. function RoomVoiceComponentView:onGVoiceEvent(event)
  349. local nSeatShowId = event.viewId
  350. local nUserId = event.nUserId
  351. local recordUrl = event.recordUrl
  352. local recordTime = event.recordTime
  353. -- 下载玩家的语音文件并播放
  354. -- 下载完成后返回该文件在本地存储的路径
  355. -- app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath)
  356. app.plugin:downloadGRecord(nUserId, recordUrl, function( filePath)
  357. -- local userId = tonumber(recordTag)
  358. local userId = tonumber(nUserId)
  359. local seatShowId = app.room:getViewIdByUserId(userId)
  360. if not seatShowId or not self.playerVoice[seatShowId] then
  361. -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId);
  362. return
  363. end
  364. self.playerVoice[seatShowId].filePath = filePath
  365. end);
  366. -- 记录该玩家的语音文件,重播要用
  367. self.playerVoice[nSeatShowId] = {}
  368. self.playerVoice[nSeatShowId].recordUrl = recordUrl;
  369. self.playerVoice[nSeatShowId].recordTime = recordTime;
  370. -- 该玩家头像上播放说话的效果
  371. self:_replayGRecord(nSeatShowId)
  372. end
  373. function RoomVoiceComponentView:onReplayVoiceEvent(event)
  374. local viewId = app.room:getViewIdByUserId(event.nUserId)
  375. self:_replayRecord(viewId)
  376. end
  377. return RoomVoiceComponentView