Android Tutorial Android Text To Speech example

Status
Not open for further replies.
Android can synthesize and play text.
Using the TTS library you can easily add this feature to your application.

tts_1.png


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
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.
 

Attachments

  • TTSExample.zip
    6.2 KB · Views: 31,421
  • TTS.apk
    69.8 KB · Views: 3,808

jmeuse2

Member
Licensed User
Longtime User
Can you revert the speech back to text to help solve format disarrangement in text reflow?

Sent from my SAMSUNG-SGH-I847 using Tapatalk 2
 

Shadow&Max

Active Member
Licensed User
Longtime User
I've got TTS working in some places in my app, but in the startup, I can never get the TTS1_Ready sub to fire on initialize. It simply executes the line and goes to the next, but it's NOT initializing there. My TTS is TTS1, the sub is TTS1_Ready, the call to it is TTS1.Initialize("TTS1"), and yet it never gets to the TTS1_Ready sub... what could possibly be wrong? I know that in this sequence, it's not being initialized anywhere else first... But I need this to work and for the life of me, I can't seem to get it to trigger.

FYI, I've closed the Genymotion emulator and started from scratch, and it still doesn't fire...

OK, I'm going to modify this a bit... I used another device from Geny, and it triggered there. Is this completely device dependent?

More... I put a messagebox in the sub to see if and when it fires, and it doesn't fire as expected. I put a breakpoint by the TTS1.Initialize("TTS1") statement... As I step through, it obviously executes that line, then goes on with the program... LATER, it hits that _Ready proc... But at a very unexpected time, and not when anything to do with Text To Speech is happening.

Any suggestions would be greatly appreciated...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I step through, it obviously executes that line, then goes on with the program... LATER, it hits that _Ready proc... But at a very unexpected time, and not when anything to do with Text To Speech is happening.
This is the expected behavior.

It prepares the text to speech service in the background and fires the Ready event when it is ready.
 

Shadow&Max

Active Member
Licensed User
Longtime User
How long should you have to wait to be able to use it Erel? If I initialize it and need to use it, is there any way to know how long it will be before I can actually have it speak? To be clear, I should do everything I want to do if it's automatically running through a list, reading items, in the Ready sub?
 

Shadow&Max

Active Member
Licensed User
Longtime User
OK, thanks Erel...
 

EvgenyB4A

Active Member
Licensed User
Longtime User
I have installed Ron Hebrew TTS on my phone, added 'he'' on language spinner at TTS code example, but after selecting "he" I get toastmessage " Language data not found"
 

EvgenyB4A

Active Member
Licensed User
Longtime User
Have you set it to be the default speech engine (under Settings - Language)?
I changed TTS engine in Settings->Language-->Speech->TTS parameters to Ron Hebrew and now the TTS example can speak Hebrew(only) in trial version.
Thank you very much first of all.
The next question is how to change TTS engines by B4A?
 

AndyW999

Member
Licensed User
Longtime User
Can TTS be used from a service module to speak an announcement instead of a ring tone when the device is 'asleep'?

Thought I would ask before trying it ;)

Thanks

Andy.
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
how can i make to read big texts ?
my internet is fast 15mb + but all big text dont read =(
 

pmsmc

Member
Licensed User
Longtime User
Hi Erel,
I am developing a application that uses both TTS and Voice recognition.
when I ask a user for and answer, using TTS, and then use VR to catch the user responde, every time I get back to TSS I get an error, saying that the Speech engine is not ready, but i have never released it form memory.
Also, is there a way to send a string to TTS and only continue the application after the TextToSpeechFinish event ?

Best Regards,
Pedro
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Status
Not open for further replies.
Top