Android can synthesize and play text.
Using the TTS library you can easily add this feature to your application.
We declared a TTS object named TTS1 as a process global object.
In Sub Activity_Resume we check if it is initialized and if not we initialize it.
The Ready event is raised when the text to speech engine is ready.
Now we enable all views which were previously disabled in the designer.
The SpeechRate and Pitch properties expect a float value. With 1 being the default.
The SeekBar returns an integer value so we divide it by 10 (its MaxValue was set to 20).
TTS1 is released in Sub Activity_Pause. This is why we need to reinitialize it in Activity_Resume.
Edit: Language is now only set when the engine is ready.
Using the TTS library you can easily add this feature to your application.
B4X:
Sub Process_Globals
Dim TTS1 As TTS
End Sub
Sub Globals
Dim barPitch As SeekBar
Dim barSpeechRate As SeekBar
Dim btnSpeak As Button
Dim EditText1 As EditText
Dim spnrLanguages As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
spnrLanguages.AddAll(Array As String("en", "fr", "de"))
End Sub
Sub TTS1_Ready (Success As Boolean)
If Success Then
'enable all views
For i = 0 To Activity.NumberOfViews - 1
Activity.GetView(i).Enabled = True
Next
btnSpeak_Click 'play first sentence
Else
Msgbox("Error initializing TTS engine.", "")
End If
End Sub
Sub Activity_Resume
If TTS1.IsInitialized = False Then
TTS1.Initialize("TTS1")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
TTS1.Release
End Sub
Sub btnSpeak_Click
If EditText1.Text.Length > 0 Then
TTS1.Speak(EditText1.Text, True)
EditText1.SelectAll
End If
End Sub
Sub barSpeechRate_ValueChanged (Value As Int, UserChanged As Boolean)
If UserChanged Then
tts1.SpeechRate = Value / 10
End If
End Sub
Sub barPitch_ValueChanged (Value As Int, UserChanged As Boolean)
If UserChanged Then
tts1.Pitch = Value / 10
End If
End Sub
Sub spnrLanguages_ItemClick (Position As Int, Value As Object)
If tts1.SetLanguage(Value, "") = False Then
ToastMessageShow("Language data not found.", True)
Return
End If
End Sub
In Sub Activity_Resume we check if it is initialized and if not we initialize it.
The Ready event is raised when the text to speech engine is ready.
Now we enable all views which were previously disabled in the designer.
The SpeechRate and Pitch properties expect a float value. With 1 being the default.
The SeekBar returns an integer value so we divide it by 10 (its MaxValue was set to 20).
TTS1 is released in Sub Activity_Pause. This is why we need to reinitialize it in Activity_Resume.
Edit: Language is now only set when the engine is ready.