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

Humberto

Active Member
Licensed User
Longtime User
I Put two Buttons.
The first button show a message when clicked and I can see the message just after the second LOG ( setlangague) appears

The second one talk just to see if it´sworking

Log
ready: 20141023 10:48:45
setlanguage: 20141023 10:48:48
 

Attachments

  • Teste_TTS.zip
    243.4 KB · Views: 513

Humberto

Active Member
Licensed User
Longtime User
I tested and works.

If I don´t use this command (setLamguage ) the language will be the default in the device. and works OK

But if someone wants to change the default language will not be able.

In your tests even with this command ( SetLanguage) works well without lock ?

I saw the command to export as zip thanks.
 

Humberto

Active Member
Licensed User
Longtime User
Erel

How can I get the default language ?

Só I don´t need to call setlangague if it already the default one
 

Humberto

Active Member
Licensed User
Longtime User
I´ll use AHLocale to get the current language and if it´s the same that the user select I´ll not use setlanguage

Thanks
 

John Woodsmall

Active Member
Licensed User
Longtime User
VR.Listen routine works fine for me here in london.
However my boss, trying to use the VR.Listen routine.
In Los angeles,ca.fails and the icon does not show for him. any ideas?
using the VR.Listen routine. says the micro-phone flickers and
then disappears.
 

rogersantosferreira

Member
Licensed User
Longtime User
Hi!

A simple question: how can I get the actual values of properties related to text to speech, like speechrate, pitch and volume?
thx in advance!
 
Status
Not open for further replies.
Top