|
|
- VoiceVo = VoiceVo or BaseClass()
- function VoiceVo:__init()
- end
-
- function VoiceVo:__defineVar()
- return {
- voice_id = 0,
- voice_path = nil,
- voice_data = nil,
- voice_clip = nil,
- is_loading = nil,
- abort_play = nil,
- last_play_start_time = nil,
- }
- end
-
- function VoiceVo:IsPlaying()
- if not self.last_play_start_time then return false end
- local leftTime = self.voice_clip.length - Status.NowTime + self.last_play_start_time
- if leftTime <= 0 then
- return false
- else
- return leftTime
- end
- end
-
- function VoiceVo:Play()
- if SystemRuntimePlatform.IsWindows() then
- return
- end
- if self.is_loading then return end
- if self.voice_clip and self.voice_clip.length ~= 0 then
- self.last_play_start_time = Status.NowTime
- ChatVoiceManager:getInstance():PlayAudioClip(self.voice_id, self.voice_clip)
- elseif self.voice_data and (not self.voice_clip or self.voice_clip.length ~= 0) then
- lua_soundM:CreateAudioClip(self.voice_data, ChatVoiceManager:getInstance():GetVoiceVersion(self.voice_id), function(ac)
- if self.abort_play then
- self.abort_play = nil
- return
- end
- if self._use_delete_method then
- return
- end
- if ac and ac ~= -1 then
- self.voice_clip = ac
- self:Play()
- else
- print("Generate audioclip by data failed!!!")
- end
- end)
- if self.voice_clip and not self._use_delete_method then
- self:Play()
- end
- elseif self.voice_path then
- local mp3Path
- if AppConst.DebugMode and not Application.isMobilePlatform then
- mp3Path = self.voice_path
- else
- mp3Path = string.gsub(self.voice_path, ".wav", ".mp3")
- SDKUtil.CallSDKFunc("ChatSpeaker", "Convert", {wavPath = self.voice_path, mp3Path = mp3Path})
- end
- self.is_loading = true
- self:StartLoadingChecker()
- local function onLoadVoiceCallback(ac)
- self:StopLoadingChecker()
- self.is_loading = nil
- if self.abort_play then
- self.abort_play = nil
- return
- end
- if self._use_delete_method then
- return
- end
- if ac and ac ~= -1 then
- if ac.length >= 1 then
- self.voice_clip = ac
- self.voice_data = data
- self:Play()
- else
- self.voice_clip = nil
- self.voice_data = nil
- Message.show("录音时间太短了!")
- end
- else
- print("load audioclip failed!!")
- end
- end
- lua_soundM:LoadVoice(mp3Path, onLoadVoiceCallback)
- elseif self.voice_clip and self.voice_clip.length == 0 then
- ChatVoiceManager:getInstance():PlayAudioClip(self.voice_id, self.voice_clip)
- else
- print("---------------VoiceVo play error--------------")
- end
- end
-
- function VoiceVo:SetVoiceData(voice_id, voice_data)
- self.voice_id = voice_id
- self.voice_data = voice_data
- end
-
- function VoiceVo:GetVoiceData(callback)
- if self.is_loading then return end
- if self.voice_path then
- local mp3Path
- if AppConst.DebugMode and not Application.isMobilePlatform then
- mp3Path = self.voice_path
- else
- mp3Path = string.gsub(self.voice_path, ".wav", ".mp3")
- SDKUtil.CallSDKFunc("ChatSpeaker", "Convert", {wavPath = self.voice_path, mp3Path = mp3Path})
- end
- self.is_loading = true
- self:StartLoadingChecker()
- local function onLoadVoiceCallback(ac, data)
- local cModel = ChatModel:getInstance()
- self:StopLoadingChecker()
- cModel.loading_voice = nil
- self.is_loading = nil
- if self._use_delete_method then
- return
- end
- if ac and ac ~= -1 then
- if ac.length >= 1 then
- self.voice_clip = ac
- self.voice_data = data
- if callback then
- callback(self.voice_data, self.voice_clip.length)
- if cModel.end_speech_data and cModel.end_speech_data ~= "" then
- Message.show(cModel.end_speech_data)
- end
- end
- else
- self.voice_clip = nil
- self.voice_data = nil
- Message.show("录音时间太短了!")
- end
- else
- print("Load Voice Failed!!!")
- end
- cModel.end_speech_data = nil
- end
- lua_soundM:LoadVoice(mp3Path, onLoadVoiceCallback)
- else
- print("Can't not load data from a nil voice path!!")
- end
- end
-
- function VoiceVo:StartLoadingChecker()
- self:StopLoadingChecker()
- ChatModel:getInstance().timer_loading = GlobalTimerQuest:AddPeriodQuest(function()
- ChatModel:getInstance().loading_voice = nil
- self.abort_play = nil
- print("Generate voice data overtime!!")
- end, 3, 1)
- end
-
- function VoiceVo:StopLoadingChecker()
- if ChatModel:getInstance().timer_loading then
- GlobalTimerQuest:CancelQuest(ChatModel:getInstance().timer_loading)
- end
- end
-
- function VoiceVo:Stop()
- if self.is_loading then
- self.abort_play = true
- else
- ChatVoiceManager:getInstance():StopAudioClip(self.voice_id, self.voice_clip)
- end
- end
-
- function VoiceVo:CheckCanDelete()
- if self.last_play_start_time and Status.NowTime - self.last_play_start_time > 10 then
- return true
- end
- return false
- end
-
- function VoiceVo:__delete()
- self:Stop()
- soundMgr:StopVoice(self.voice_id, self.voice_clip)
- self.voice_data = nil
- self.voice_clip = nil
- end
|