Android Tutorial Android Text To Speech example

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Android can synthesize and play text.
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
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

Cor

Active Member
Licensed User
Works great :)

Going to use this for my guitar program

thanks

now only missing the jet midi library :)

grCor
 

Cor

Active Member
Licensed User
I also get language data not found,

But text to speech is working

EDIT: I adjust the program, so it first select the language

btw. I installed svox tts engine for the Netherlands and Frence, works perfectly

grCor
 
Last edited:

susu

Well-Known Member
Licensed User
Just a silly question: I got TTS for Vietnamese written in C#. Is there a chance to use it on Android?
 

ZJP

Active Member
Licensed User
I installed Svox TTS Engine with French voice. Not working. :(
Any help !!!

JP
 

Cor

Active Member
Licensed User
I have installed svox engish and france

works great.

You must set svox as default in settings in your phone

then you can easily switch in b4a to to english and france

grCor
 

nfordbscndrd

Well-Known Member
Licensed User
I have an Archos 70-250G 7" tablet. When I tried to compile-run the TTS tutorial to the A70, it rebooted and would not install. I then set up my standard 800x480 7" emulator and it also rebooted during installation and never came back up.

FWIW, I see two TTS's on the A70: Pico TTS and eSpeakTTS, but only Pico TTS shows up in the menu for selecting a default speech synthesis engine.

Below is the last part of the log dump from the emulator. I'm sending this because I see a lot of "not found", "unable to open", and "unknown permission" messages.

Another app on here (I think it was "Hubble") also caused a reboot during installation and nobody ever figured out why. Can you tell anything from this log?

 

nfordbscndrd

Well-Known Member
Licensed User
The permissions warnings do not seem to be related to the installed application.
I'm pretty sure that it installed on the emulator properly when I tested it. The Android emulator is far from being perfect, and it fails from time to time.
Before it bombed on the emulator, it had caused my Archos 70 to reboot when I tried to compile-run to it. However, as I just mentioned in another thread, this and some other "problem" apps suddenly started compiling/installing/running okay on both the emulator and the A70, so I guess it will remain a mystery.

Thanks.
 

schimanski

Well-Known Member
Licensed User
Hello!

The TTS-Lib can be very useful for my app, but I don't want to risk high system resources. Does the voice response runs in a seperate thread? And what about the computational power? Under windows mobile, I had the sensation, that the speechengine takes a lot of CPU-speed...

Thanks for an answer...
 
Status
Not open for further replies.
Top