源战役客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

181 řádky
4.7 KiB

před 4 týdny
  1. VoiceVo = VoiceVo or BaseClass()
  2. function VoiceVo:__init()
  3. end
  4. function VoiceVo:__defineVar()
  5. return {
  6. voice_id = 0,
  7. voice_path = nil,
  8. voice_data = nil,
  9. voice_clip = nil,
  10. is_loading = nil,
  11. abort_play = nil,
  12. last_play_start_time = nil,
  13. }
  14. end
  15. function VoiceVo:IsPlaying()
  16. if not self.last_play_start_time then return false end
  17. local leftTime = self.voice_clip.length - Status.NowTime + self.last_play_start_time
  18. if leftTime <= 0 then
  19. return false
  20. else
  21. return leftTime
  22. end
  23. end
  24. function VoiceVo:Play()
  25. if SystemRuntimePlatform.IsWindows() then
  26. return
  27. end
  28. if self.is_loading then return end
  29. if self.voice_clip and self.voice_clip.length ~= 0 then
  30. self.last_play_start_time = Status.NowTime
  31. ChatVoiceManager:getInstance():PlayAudioClip(self.voice_id, self.voice_clip)
  32. elseif self.voice_data and (not self.voice_clip or self.voice_clip.length ~= 0) then
  33. lua_soundM:CreateAudioClip(self.voice_data, ChatVoiceManager:getInstance():GetVoiceVersion(self.voice_id), function(ac)
  34. if self.abort_play then
  35. self.abort_play = nil
  36. return
  37. end
  38. if self._use_delete_method then
  39. return
  40. end
  41. if ac and ac ~= -1 then
  42. self.voice_clip = ac
  43. self:Play()
  44. else
  45. print("Generate audioclip by data failed!!!")
  46. end
  47. end)
  48. if self.voice_clip and not self._use_delete_method then
  49. self:Play()
  50. end
  51. elseif self.voice_path then
  52. local mp3Path
  53. if AppConst.DebugMode and not Application.isMobilePlatform then
  54. mp3Path = self.voice_path
  55. else
  56. mp3Path = string.gsub(self.voice_path, ".wav", ".mp3")
  57. SDKUtil.CallSDKFunc("ChatSpeaker", "Convert", {wavPath = self.voice_path, mp3Path = mp3Path})
  58. end
  59. self.is_loading = true
  60. self:StartLoadingChecker()
  61. local function onLoadVoiceCallback(ac)
  62. self:StopLoadingChecker()
  63. self.is_loading = nil
  64. if self.abort_play then
  65. self.abort_play = nil
  66. return
  67. end
  68. if self._use_delete_method then
  69. return
  70. end
  71. if ac and ac ~= -1 then
  72. if ac.length >= 1 then
  73. self.voice_clip = ac
  74. self.voice_data = data
  75. self:Play()
  76. else
  77. self.voice_clip = nil
  78. self.voice_data = nil
  79. Message.show("录音时间太短了!")
  80. end
  81. else
  82. print("load audioclip failed!!")
  83. end
  84. end
  85. lua_soundM:LoadVoice(mp3Path, onLoadVoiceCallback)
  86. elseif self.voice_clip and self.voice_clip.length == 0 then
  87. ChatVoiceManager:getInstance():PlayAudioClip(self.voice_id, self.voice_clip)
  88. else
  89. print("---------------VoiceVo play error--------------")
  90. end
  91. end
  92. function VoiceVo:SetVoiceData(voice_id, voice_data)
  93. self.voice_id = voice_id
  94. self.voice_data = voice_data
  95. end
  96. function VoiceVo:GetVoiceData(callback)
  97. if self.is_loading then return end
  98. if self.voice_path then
  99. local mp3Path
  100. if AppConst.DebugMode and not Application.isMobilePlatform then
  101. mp3Path = self.voice_path
  102. else
  103. mp3Path = string.gsub(self.voice_path, ".wav", ".mp3")
  104. SDKUtil.CallSDKFunc("ChatSpeaker", "Convert", {wavPath = self.voice_path, mp3Path = mp3Path})
  105. end
  106. self.is_loading = true
  107. self:StartLoadingChecker()
  108. local function onLoadVoiceCallback(ac, data)
  109. local cModel = ChatModel:getInstance()
  110. self:StopLoadingChecker()
  111. cModel.loading_voice = nil
  112. self.is_loading = nil
  113. if self._use_delete_method then
  114. return
  115. end
  116. if ac and ac ~= -1 then
  117. if ac.length >= 1 then
  118. self.voice_clip = ac
  119. self.voice_data = data
  120. if callback then
  121. callback(self.voice_data, self.voice_clip.length)
  122. if cModel.end_speech_data and cModel.end_speech_data ~= "" then
  123. Message.show(cModel.end_speech_data)
  124. end
  125. end
  126. else
  127. self.voice_clip = nil
  128. self.voice_data = nil
  129. Message.show("录音时间太短了!")
  130. end
  131. else
  132. print("Load Voice Failed!!!")
  133. end
  134. cModel.end_speech_data = nil
  135. end
  136. lua_soundM:LoadVoice(mp3Path, onLoadVoiceCallback)
  137. else
  138. print("Can't not load data from a nil voice path!!")
  139. end
  140. end
  141. function VoiceVo:StartLoadingChecker()
  142. self:StopLoadingChecker()
  143. ChatModel:getInstance().timer_loading = GlobalTimerQuest:AddPeriodQuest(function()
  144. ChatModel:getInstance().loading_voice = nil
  145. self.abort_play = nil
  146. print("Generate voice data overtime!!")
  147. end, 3, 1)
  148. end
  149. function VoiceVo:StopLoadingChecker()
  150. if ChatModel:getInstance().timer_loading then
  151. GlobalTimerQuest:CancelQuest(ChatModel:getInstance().timer_loading)
  152. end
  153. end
  154. function VoiceVo:Stop()
  155. if self.is_loading then
  156. self.abort_play = true
  157. else
  158. ChatVoiceManager:getInstance():StopAudioClip(self.voice_id, self.voice_clip)
  159. end
  160. end
  161. function VoiceVo:CheckCanDelete()
  162. if self.last_play_start_time and Status.NowTime - self.last_play_start_time > 10 then
  163. return true
  164. end
  165. return false
  166. end
  167. function VoiceVo:__delete()
  168. self:Stop()
  169. soundMgr:StopVoice(self.voice_id, self.voice_clip)
  170. self.voice_data = nil
  171. self.voice_clip = nil
  172. end