Sub JobDone (Job As HttpJob)
If Job.Tag="ListAllAvailableVoices" Then
'For listing the available voices
If Job.Success Then
Try
Dim jsonParser As JSONParser
jsonParser.Initialize(Job.GetString)
Dim mapVoiceData As Map=jsonParser.NextObject
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_AvailableVoices", mapVoiceData)
Catch
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_AvailableVoices", Null)
End Try
Else
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_AvailableVoices", Null)
End If
Else If Job.Tag="speak" Then
'For speaking the text
If Job.Success Then
Try
Dim jsonParser As JSONParser
jsonParser.Initialize(Job.GetString)
Dim strAudio As String=jsonParser.NextObject.Get("audioContent")
Dim su As StringUtils ' jStringUtils
Dim decoded() As Byte = su.DecodeBase64(strAudio) 'Now save the mp3
#IF B4A
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "CloudTTS.mp3", False)
#Else IF B4J
Dim out As OutputStream = File.OpenOutput(File.DirApp, getFileName(strPrevToSpeak)&" "&LanguageCode&"-"&LanguageName&".mp3", False)
#End If
out.WriteBytes(decoded, 0, decoded.Length)
out.Close
'Now, play the audio
#IF B4A
audio.Load(File.DirInternal, "CloudTTS.mp3")
#Else IF B4J
audio=AudioClip_Static.NewAudioClip2(File.DirApp, getFileName(strPrevToSpeak)&" "&LanguageCode&"-"&LanguageName&".mp3")
audio.SetCompleteListener(Me, "audio")
#End If
audio.Play
CallSubDelayed(CloudTTS_Parent, CloudTTS_Event_Name & "_StartedSpeaking")
Catch
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_onError", LastException.Message)
End Try
Else
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_onError", Job.ErrorMessage)
End If
Else If Job.Tag="ConvertToMP3" Then
'For creating an MP3
If Job.Success Then
Try
Dim jsonParser As JSONParser
jsonParser.Initialize(Job.GetString)
Dim strAudio As String=jsonParser.NextObject.Get("audioContent")
Dim su As StringUtils ' jStringUtils
Dim decoded() As Byte = su.DecodeBase64(strAudio) 'Now save the mp3
Dim out As OutputStream = File.OpenOutput(fd, fn, False)
out.WriteBytes(decoded, 0, decoded.Length)
out.Close
Catch
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_onError", LastException.Message)
End Try
Else
CallSubDelayed2(CloudTTS_Parent, CloudTTS_Event_Name & "_onError", Job.ErrorMessage)
End If
audio_Complete
End If
Job.Release
End Sub