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,402
  • TTS.apk
    69.8 KB · Views: 3,790

jota

Active Member
Licensed User
Longtime User
Hello, it would be possible to incorporate the command [synthesizeToFile] to the library?. Thanks
---------------------------------------------------------------------------------------
Hola, seria posible incorporar el comando [synthesizeToFile] a la libreria?. Gracias
 

jasonvch

New Member
Licensed User
Longtime User
Is there any way to check whether the TTS engine finished speaking the text or not???

thanks
 

squaremation

Active Member
Licensed User
Longtime User
I keep recieving this error 'Field barpitch was declared with the wrong wrong type'

Fixed: for some reason I was not paying attention and added progress bars instead of seekbars :sign0161:
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Is the speech engine something that doesn't meet Android licensing requirements like Google Apps? I have a Motorola Tablet with stock Android (No Google Apps) that this doesn't work on. It doesn't have Voice Recognition either (No Mic on Keyboard). There is no market on the device either for a way to get them, so I guess I need to find an apk for them somewhere.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
If Provided by Google it may not be in stock/pure just like their apps. Their apps have a license fee and some devices are Pure Android which doesn't include Google Apps or any other applications not licensed under the proper guidlines. While many associate Google with Android, they are just a member of the group. They provide their apps for a fee and are not part of Pure Android unfortunately. Are there any that meet those standards that could be installed without Google licenses?
 

johnaaronrose

Active Member
Licensed User
Longtime User
Other languages

I noted that the example allows selection of other languages. However, it speaks text already in another language. Is it possible to call up an automated translator (e.g. to translate from, for example, to French) before the text is spoken?
 

sorex

Expert
Licensed User
Longtime User
Erel:

Are you saying that speech is included in the OS these days in the newer models?

I upgraded my phone to 2.2 a month ago but the default TTS engine is Pico TTS,
I guess I installed it for Endomondo but when I select in the app example french it still spits out english
while the pico shows 6 or 7 installed languages including french.

I still need dutch but searching the market for Pico or Pico TTS doesn't give any results anymore.
 

johnaaronrose

Active Member
Licensed User
Longtime User
Service

Has anybody written a B4A app/example which includes a language translation service?
 

LouFromDetroit

Member
Licensed User
Longtime User
Other Languages Possible?

Hi - 2 questions:

1-How do I add another language to the TTS example like spanish or italian?

2-It seems that the pitch and rate settings only affect the english voice and not the other 2. What could explain this?

Thanks
 

LouFromDetroit

Member
Licensed User
Longtime User
Other Languages Possible? answer

I answered my own question #2:
I see that I needed to install the German and French google lang in settings and now the pitch/rate works. Strange that it kind of worked before the install.

As soon as I find the language codes exposed for the other langauges I can see this coming in handy for VR testing or traveling.
 
Status
Not open for further replies.
Top