Android Question highlight text and text to speech

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with this code:
B4X:
Sub Globals
   Private tts As TTS
   Private jtts As JavaObject
   Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   tts.Initialize("tts")
   Activity.LoadLayout("1")
   jtts = tts
   Dim e As Object = jtts.CreateEvent("android.speech.tts.TextToSpeech.OnUtteranceCompletedListener", "OnUtterance", Null)
   jtts.RunMethod("setOnUtteranceCompletedListener", Array(e))
End Sub

Sub OnUtterance_Event (MethodName As String, Args() As Object) As Object
   Log(Args(0))
   Return Null
End Sub

Sub TTS_Ready (Success As Boolean)
   If Success Then
     Button1.Enabled = True
   End If
End Sub

Sub Button1_Click
   Speak("This", "1")
   Speak("is", "2")
   Speak("a", "3")
   Speak("This", "4")
End Sub

Private Sub Speak(Word As String, Id As String)
   Dim m As JavaObject
   m.InitializeNewInstance("java.util.HashMap", Null)
   m.RunMethod("put", Array("utteranceId", Id))
   jtts.RunMethod("speak", Array(Word, 1, m))
End Sub

The OnUtterance_Event will be raised after each word. You can use it together with RichString to highlight the current word. The downside is that the text spoken in less fluent.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
why i get this
B4A version: 5.80
Parsing code. Error
Error parsing program.
Error description: Unknown type: javaobject
Are you missing a library reference?
Occurred on line: 16 (Main)
Private jtts As JavaObject

what is missing?
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Javaobject is probably missing.
Tick it in the library pane on the right.
 
Upvote 0
Top