#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim sr As SpeechToText
Dim ri As RecognizerIntent
Dim i As Intent
Dim tt As TTS
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim EditText1,edittext2 As EditText
Dim pronto=False As Boolean
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("1")
EditText1.Initialize("")
EditText1.SingleLine=False
EditText1.Gravity=Gravity.TOP
Activity.AddView(EditText1,0,0,100%x,50%y-5dip)
edittext2.Initialize("")
edittext2.SingleLine=False
edittext2.Gravity=Gravity.TOP
Activity.AddView(edittext2,0,50%y+5dip,100%x,100%y-10dip)
tt.Initialize("tt")
tt.SetLanguage("pt","PT")
sr.initialize("sr")
i.Initialize( ri.ACTION_VOICE_SEARCH_HANDS_FREE,"")
i.PutExtra(ri.EXTRA_CALLING_PACKAGE,"com.inforpires.testSpeech")
i.PutExtra(ri.EXTRA_LANGUAGE_MODEL, ri.LANGUAGE_MODEL_FREE_FORM)
i.PutExtra(ri.EXTRA_LANGUAGE,"pt_PT")
i.PutExtra(ri.EXTRA_MAX_RESULTS,3)
i.PutExtra(ri.EXTRA_PARTIAL_RESULTS,True)
' traduz("fogo")
End Sub
Sub tt_Ready (Success As Boolean)
pronto = Success
End Sub
Sub sr_onResults(results As Object)
Dim frase As String
frase=""
Dim words As List
words.Initialize2(results)
For Each word As String In words
frase = frase & word
Next
EditText1.Text = frase & CRLF & EditText1.text
If pronto Then
traduz(frase)
End If
sr.startListening(i)
End Sub
Sub sr_onError(erro As Int)
Log("ERROR : " & erro)
If erro=8 Then
sr.stopListening
sr.destroy
sr.createSpeechRecognizer
sr.startListening(i)
Else
sr.startListening(i)
End If
End Sub
Sub sr_onEndOfSpeech
Log("End of speech")
'sr.startListening(i)
End Sub
Sub sr_onRmsChanged(rmsdB As Float)
'Log("RMS" & rmsdB)
End Sub
Sub Activity_Resume
If sr.isRecognitionAvailable Then
sr.createSpeechRecognizer
sr.startListening(i)
Log("OK")
Else
Log("Not OK")
End If
If tt.IsInitialized=False Then
pronto=False
tt.Initialize("tt")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
sr.destroy
tt.Release
End Sub
Sub traduz(frase As String)
Dim o As HttpJob
o.Initialize("traducao",Me)
o.Download2("http://api.mymemory.translated.net/get", Array As String("q",frase,"langpair","pt|en"))
End Sub
Sub jobDone(job As HttpJob)
Dim s As String
If job.Success Then
s=job.GetString
job.Release
finalTraducao(s)
Else
job.Release
Log("error in response")
End If
End Sub
Sub finalTraducao(s As String)
Dim j As JSONParser
Dim m,m1 As Map
Dim traduzido As String
j.Initialize(s)
m=j.NextObject
m1=m.Get("responseData")
fala(m1.Get("translatedText"))
End Sub
Sub fala(s As String)
edittext2.text = s & CRLF & edittext2.Text
If pronto Then
tt.Speak(s,False)
End If
End Sub