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: 30,745
  • TTS.apk
    69.8 KB · Views: 3,289

pmsmc

Member
Licensed User
Longtime User
Thanks Erel, going to try that,
and what about the TTS error, after I use VR ?
 

pmsmc

Member
Licensed User
Longtime User
I get this error
java.lang.RuntimeException: Method: IsSpeaking not found in: android.speech.tts.TextToSpeech
 

Humberto

Active Member
Licensed User
Longtime User
I´m using the TTS library in my program but it lock the software for about 3 seconds until is ready.
I don´t need it to run the program just in some cases that the user press to listen a contact name.
I tried to load the library in a thread but still lock the program for 3 seconds.
There is any other way to load in background ?

<code>
If TTS1.IsInitialized = False Then
Dim args(0) As Object
Thread_TTS.Initialise("Thread_TTS")
Thread_TTS.Name = "Thread TTS"
Thread_TTS.Priority = Thread_TTS.MinPriority
Thread_TTS.Start(Me, "threadSub_1", args) 'o nome da thread tem que ter undescore
End If

Sub threadSub_1
If TTS1.IsInitialized = False Then
TTS1.Initialize("TTS1")
End If
end sub
</code>
 

Humberto

Active Member
Licensed User
Longtime User
I build a list with received calls and without loading the library just after the program starts I can scroll the list, but when I initialize the library the scroll starts after 3 seconds
 

Humberto

Active Member
Licensed User
Longtime User
Erel
I put the software in "Share your creation" and you can download from Play Store.
Now, for default, it is disabled the option to talk the contact and receveid calls but you can enable in configuration favorites sound and Register Sound.
You will be able to see the difference between load the library and not.
Ypu must change the language
 

Humberto

Active Member
Licensed User
Longtime User
I wrote I project that build a list view with the calls received and if I load the TTS library still take more than 3 seconds to be able to scroll the list.
If I don´t load the library the scroll start instantly.

The english library took more time than portuguese library
 

Attachments

  • Teste_TTS.zip
    494.3 KB · Views: 316

Humberto

Active Member
Licensed User
Longtime User
Erel

there was some library that I didn´t used.

Now need just one library from outside please send your e-mail to send it . I already have the permission to send to you
 

Attachments

  • Teste_TTS.zip
    432.2 KB · Views: 286

Humberto

Active Member
Licensed User
Longtime User
Ok
Now just build a listview.
If I try to initialize a TTS I can not scroll the list just after start.
 

Attachments

  • Teste_TTS.zip
    241.7 KB · Views: 261

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a mistake to set the language before TTS is initialized.

Test with this code:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim listView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Main")
  listView1.Initialize("ListView1")
  For i = 1 To 300
  listView1.AddSingleLine("Item #" & i)
  Next
  Activity.AddView(listView1, 0, 0, 100%x, 100%y)
End Sub

Sub Activity_Resume
   If TTS1.IsInitialized = False Then
     TTS1.Initialize("TTS1")
   End If
End Sub

Sub TTS1_Ready (Success As Boolean)
   Log("ready")
   If Success  Then
'     TTS1.SpeechRate = 1
'     TTS1.Pitch = 2
     If TTS1.SetLanguage("en" , "") = False Then  'en.fr,de
       ToastMessageShow( "TTS library not found", True)
       Return
     End If
'para falar
'     TTS1.Speak(EditText1.Text, True)
   Else
     Msgbox("Error initializing TTS engine.", "")
   End If
End Sub

I wasn't able to reproduce it as it takes a few milliseconds for the Ready event to be raised.
 

Humberto

Active Member
Licensed User
Longtime User
I tested, but still the same problem.

I put another LOG after the language setup and we can see that takes 4 to 5 seconds ( samsung galaxy S3 ) to do the setup and during that time the activity is locked


B4X:
Sub TTS1_Ready (Success As Boolean)
   DateTime.DateFormat = "yyyyMMdd HH:mm:ss"
   Log("ready: " &  DateTime.Date(DateTime.Now))
   If Success  Then
'     TTS1.SpeechRate = 1
'     TTS1.Pitch = 2
     If TTS1.SetLanguage("en" , "") = False Then  'en.fr,de
       ToastMessageShow( "TTS library not found", True)
       Return
     End If
    Log("setlanguage: " &  DateTime.Date(DateTime.Now))

   Else
     Msgbox("Error initializing TTS engine.", "")
   End If

End Sub

There is anything to do ?
 

Humberto

Active Member
Licensed User
Longtime User
I Tested in LG -II dual chip ( android 4.12 same as Galaxy S3). if "IsFirst = True" takes almost 5 seconds between the messages, but if I call again and IsFirst = False then there is no difference between the messages.

In Galaxy S3 always takes 3 seconds between the messages ( "ready" and "setlanguage" Logs)
 
Status
Not open for further replies.
Top