-- 聊天组件 配合RoomChatView使用 local ChatDefine = require("luaScript.GameChatDefine") local RoomVoiceComponentView = class("RoomVoiceComponentView", cc.UIView) function RoomVoiceComponentView:ctor(btnVoice) RoomVoiceComponentView.super.ctor(self) self:loadUI() self.btnVoice = btnVoice self.playerVoice = {} -- 我的视图椅子号 self.myViewId = app.room:getViewIdByUserId(app.user.loginInfo.uid) or 1 --系统的音效状态 self.soundState = app.systemSetting.info.sound self.recordingTime = os.time() end function RoomVoiceComponentView:loadUI() local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_voice.ui") self.ui = ui self:addChild(ui) end function RoomVoiceComponentView:onEnter() RoomVoiceComponentView.super.onEnter(self) self:initBindEvent() --初始voice按钮事件 if self.btnVoice and not tolua.isnull(self.btnVoice) then -- self.btnVoice:registerClick(handler(self , self.stopRecord), handler(self , self.beginRecord), handler(self , self.stopRecord)) self.btnVoice:registerClick(handler(self , self.stopGRecord), handler(self , self.beginGRecord), handler(self , self.stopGRecord)) end --默认隐藏 self:hideSpeaking() end -- 显示屏幕中间滑动手指取消录音的提示 function RoomVoiceComponentView:showSpeaking() logD("RoomVoiceComponentView:showSpeaking") self.ui.Items.Layout_luyinzhong:setVisible(true) self.ui.Items.Text_Tips:setText("") if isAndroidPlatform() then self.ui.Items.Layout_luyinzhong:playClip("speaking") elseif isIOSPlatform() then self.ui.Items.Text_Tips:setText("正在开启录音设备") self.ui.Items.Layout_3:setVisible(false); runDelay(1.1, function () logD("RoomVoiceComponentView:runDelay") self.ui.Items.Text_Tips:setText("") self.ui.Items.Layout_3:setVisible(true); self.ui.Items.Layout_luyinzhong:playClip("speaking") end) end end -- 隐藏屏幕中间滑动手指取消录音的提示 function RoomVoiceComponentView:hideSpeaking() self.ui.Items.Layout_luyinzhong:setVisible(false) self.ui.Items.Layout_luyinzhong:stopClip("speaking") end -- 开始录音 function RoomVoiceComponentView:beginRecord() local t = os.time() - self.recordingTime if not app.user:canSendChatMessage(2) or t < ChatDefine.TimeInterval[2] then showTooltip("您发送语音信息的间隔时间太短,请稍后再发!") return end if self.recording then return end -- 停止所有的表情声音 -- if self.lastSound then -- stopVoice(self.lastSound) -- end --为了不影响录制的过程,还可以听到音效,关闭所有的音效 app.systemSetting.info.sound = false --暂停音乐 cc.AudioController:getInstance():pause(); --暂停音效 --cc.AudioController:getInstance():setPlaySound(false) -- 停止所有的玩家语音 app.plugin:stopPlayRecord() -- 如果有玩家正在说话 if self.lastSpeakingSeatId then app.user:dispatchEvent({name = GAME_EVENT.CHAT_STOP_VOICE , viewId = self.lastSpeakingSeatId}) -- self:shopVoice(self.lastSpeakingSeatId) end self.recording = true; self.recordingTime = os.time() --showTooltip("开始录音") self:showSpeaking() app.plugin:startRecord() end -- 开始录音,腾讯语音GVoice function RoomVoiceComponentView:beginGRecord() local t = os.time() - self.recordingTime if t < ChatDefine.TimeInterval[2] then showTooltip("您发送语音信息的间隔时间太短,请稍后再发!") return end if self.recording then return end --为了不影响录制的过程,还可以听到音效,关闭所有的音效 app.systemSetting.info.sound = false --暂停音乐 cc.AudioController:getInstance():pause(); -- 停止所有的玩家语音 app.plugin:stopGPlayRecord() -- 如果有玩家正在说话, todol 下面这个以后再改 if self.lastSpeakingSeatId then app.user:dispatchEvent({name = GAME_EVENT.CHAT_STOP_VOICE , viewId = self.lastSpeakingSeatId}) end self.recording = true; self.recordingTime = os.time() self:showSpeaking() app.plugin:startGRecord() end -- 结束录音 function RoomVoiceComponentView:stopRecord() if not self.recording then return end -- showTooltip("结束录音111") self.recording = false; self:hideSpeaking(); app.systemSetting.info.sound = self.soundState --继续音乐 cc.AudioController:getInstance():resume(); --恢复音效 --cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound) if cc.Application:getInstance():getTargetPlatform() == 0 then local jsonData = { recordUrl = "recordUrl", recordTime = 2, } local content = json.encode(jsonData) if app.user:sendChatMessage(2, content) then -- 记录我发送过的语音 self.playerVoice[self.myViewId] = {} self.playerVoice[self.myViewId].filePath = "" self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl self.playerVoice[self.myViewId].recordTime = jsonData.recordTime; -- 播放我的语音 self:_replayRecord(self.myViewId) end return end app.plugin:stopRecord() end -- 结束录音,腾讯语音GVoice function RoomVoiceComponentView:stopGRecord() if not self.recording then return end self.recording = false; self:hideSpeaking(); app.systemSetting.info.sound = self.soundState --继续音乐 cc.AudioController:getInstance():resume(); if cc.Application:getInstance():getTargetPlatform() == 0 then local jsonData = { -- recordUrl = "recordUrl", recordUrl = "fileId", recordTime = 2, } local content = json.encode(jsonData) if app.user:sendChatMessage(2, content) then -- 记录我发送过的语音 self.playerVoice[self.myViewId] = {} self.playerVoice[self.myViewId].filePath = "" self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl self.playerVoice[self.myViewId].recordTime = jsonData.recordTime; -- 播放我的语音 self:_replayGRecord(self.myViewId) end return end app.plugin:stopGRecord() end -- 取消录音 function RoomVoiceComponentView:cancelRecord() if not self.recording then return end --继续音乐 cc.AudioController:getInstance():resume(); --恢复音效 --cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound) app.systemSetting.info.sound = self.soundState --showTooltip("取消录音") self.recording = false; self:hideSpeaking(); app.plugin:cancelRecord() end -- 播放回调函数 function RoomVoiceComponentView:recordCallback(event) --恢复音乐 if app.systemSetting.info.music then cc.AudioController:getInstance():resume(); end if not event then return end local filePath, recordUrl, recordTime = event.filePath, event.recordUrl, event.recordTime log("log_event", filePath, recordUrl, recordTime) if not filePath or not recordUrl or not recordTime then return end recordTime = math.ceil(recordTime / 1000); --腾讯语音GVoice 开启录音设备大概有1秒延迟 if isIOSPlatform() then recordTime = recordTime - 1; end local jsonData = { recordUrl = recordUrl, recordTime = recordTime, } if recordTime<=1 then showTooltip("录音时间过短!") return end local config=getSubGameConfig(tonumber(app.gameId)) if config and config.belongType==2 then local nMyUserId = app.user.loginInfo.uid self.myViewId = app.room:getViewIdByUserId(nMyUserId) end local content = json.encode(jsonData); if app.user:sendChatMessage(2, content) then -- 记录我发送过的语音 self.playerVoice[self.myViewId] = {} self.playerVoice[self.myViewId].filePath = filePath; self.playerVoice[self.myViewId].recordUrl = recordUrl; self.playerVoice[self.myViewId].recordTime = recordTime; -- 播放我的语音 self:_replayRecord(self.myViewId) end end -- 播放回调函数 function RoomVoiceComponentView:recordGCallback(event) --恢复音乐 if app.systemSetting.info.music then cc.AudioController:getInstance():resume(); end if not event then return end local fileId, filePath, recordTime = event.fileId, event.filePath, event.recordTime log("log_event", fileId, filePath, recordTime) if not fileId or not filePath or not recordTime then return end recordTime = math.ceil(recordTime / 1); local jsonData = { recordUrl = fileId, recordTime = recordTime, } if recordTime<=1 then showTooltip("录音时间过短!") return end local config=getSubGameConfig(tonumber(app.gameId)) if config and config.belongType==2 then local nMyUserId = app.user.loginInfo.uid self.myViewId = app.room:getViewIdByUserId(nMyUserId) end local content = json.encode(jsonData); if app.user:sendChatMessage(2, content) then -- 记录我发送过的语音 self.playerVoice[self.myViewId] = {} self.playerVoice[self.myViewId].filePath = filePath; self.playerVoice[self.myViewId].recordUrl = fileId; self.playerVoice[self.myViewId].recordTime = recordTime; -- 播放我的语音 self:_replayGRecord(self.myViewId) end end -- 重新播放某个玩家之前说的话 function RoomVoiceComponentView:_replayRecord(nSeatShowId) log("RoomVoiceComponentView:_replayRecord() nSeatShowId = ", nSeatShowId); log("RoomVoiceComponentView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice)); local data = self.playerVoice[nSeatShowId] if data then if data.filePath then app.plugin:playRecord(data.filePath) end local recordTime = data.recordTime self.lastSpeakingSeatId = nSeatShowId app.user:dispatchEvent({name = GAME_EVENT.CHAT_PLAY_VOICE , viewId = nSeatShowId,recordTime = recordTime}) -- self:playVoice(nSeatShowId, recordTime, function() -- self.lastSpeakingSeatId = nil -- end) else showTooltip("无历史消息") end end -- 重新播放某个玩家之前说的话,Gvoice function RoomVoiceComponentView:_replayGRecord(nSeatShowId) log("RoomVoiceComponentView:_replayRecord() nSeatShowId = ", nSeatShowId); log("RoomVoiceComponentView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice)); local data = self.playerVoice[nSeatShowId] if data then if data.filePath then app.plugin:playGRecord(data.filePath) end local recordTime = data.recordTime self.lastSpeakingSeatId = nSeatShowId app.user:dispatchEvent({name = GAME_EVENT.CHAT_PLAY_VOICE , viewId = nSeatShowId,recordTime = recordTime}) -- self:playVoice(nSeatShowId, recordTime, function() -- self.lastSpeakingSeatId = nil -- end) else showTooltip("无历史消息") end end function RoomVoiceComponentView:initBindEvent() -- 录制完成回调 self:bindEvent(app , "recordCallback" , handler(self , self.recordCallback)) -- 录制完成回调,Gvoice self:bindEvent(app , "recordGCallback" , handler(self , self.recordGCallback)) --收到语音消息 self:bindEvent(app.user, GAME_EVENT.CHAT_VOICE_RESP, handler(self, self.onVoiceEvent)) --收到语音消息,Gvoice self:bindEvent(app.user, GAME_EVENT.CHAT_VOICE_RESP, handler(self, self.onGVoiceEvent)) self:bindEvent(app.user, GAME_EVENT.CHAT_REPLAY_VOICE, handler(self, self.onReplayVoiceEvent)) --绑定change音效 self:bindEvent(app , "onChangeYinXiao" , handler(self , self.onChangeYinXiao)); end function RoomVoiceComponentView:onChangeYinXiao(data) self.soundState = app.systemSetting.info.sound end function RoomVoiceComponentView:onVoiceEvent(event) local nSeatShowId = event.viewId local nUserId = event.nUserId local recordUrl = event.recordUrl local recordTime = event.recordTime -- 下载玩家的语音文件并播放 -- 下载完成后返回该文件在本地存储的路径 app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath) local userId = tonumber(recordTag) local seatShowId = app.room:getViewIdByUserId(userId) if not seatShowId or not self.playerVoice[seatShowId] then -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId); return end self.playerVoice[seatShowId].filePath = filePath end); -- 记录该玩家的语音文件,重播要用 self.playerVoice[nSeatShowId] = {} self.playerVoice[nSeatShowId].recordUrl = recordUrl; self.playerVoice[nSeatShowId].recordTime = recordTime; -- 该玩家头像上播放说话的效果 self:_replayRecord(nSeatShowId) end function RoomVoiceComponentView:onGVoiceEvent(event) local nSeatShowId = event.viewId local nUserId = event.nUserId local recordUrl = event.recordUrl local recordTime = event.recordTime -- 下载玩家的语音文件并播放 -- 下载完成后返回该文件在本地存储的路径 -- app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath) app.plugin:downloadGRecord(nUserId, recordUrl, function( filePath) -- local userId = tonumber(recordTag) local userId = tonumber(nUserId) local seatShowId = app.room:getViewIdByUserId(userId) if not seatShowId or not self.playerVoice[seatShowId] then -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId); return end self.playerVoice[seatShowId].filePath = filePath end); -- 记录该玩家的语音文件,重播要用 self.playerVoice[nSeatShowId] = {} self.playerVoice[nSeatShowId].recordUrl = recordUrl; self.playerVoice[nSeatShowId].recordTime = recordTime; -- 该玩家头像上播放说话的效果 self:_replayGRecord(nSeatShowId) end function RoomVoiceComponentView:onReplayVoiceEvent(event) local viewId = app.room:getViewIdByUserId(event.nUserId) self:_replayRecord(viewId) end return RoomVoiceComponentView