Android Tutorial Android Text To Speech example

Discussion in 'Tutorials & Examples' started by Erel, Dec 23, 2010.

  1. Erel

    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.

    [​IMG]

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

    Attached Files:

    Phayao, navidm, Sytek and 1 other person like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. agraham

    agraham Expert Licensed User

    I assume my device (ZTE-Blade/Orange San Francisco) must be missing something as all I get are "Language data not found" toasts. :(
     
  4. Cor

    Cor Active Member Licensed User

    Works great :)

    Going to use this for my guitar program

    thanks

    now only missing the jet midi library :)

    grCor
     
  5. agraham

    agraham Expert Licensed User

    A quick Google later and I find my device doesn't have the speech data installed but has a setting to download it from the market when it gets installed on the SD card. Works fine now!
     
    Last edited: Dec 23, 2010
  6. Erel

    Erel Administrator Staff Member Licensed User

    "1 2 3 4 5 6 7 8 9 10"
    Time to learn counting in other languages now...
     
  7. Cor

    Cor Active Member Licensed User

    I assume speech recognition is not far away......
     
  8. Cor

    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: Dec 23, 2010
  9. susu

    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?
     
  10. ZJP

    ZJP Active Member Licensed User

    @Erel Thx :sign0060:

    JP
     
    Last edited: Dec 23, 2010
  11. Erel

    Erel Administrator Staff Member Licensed User

    You can change the engine used in the device settings. However it should be compatible with Android.
     
  12. ZJP

    ZJP Active Member Licensed User

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

    JP
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Does it work with other languages?
    Did you played with the device "text to speech" settings?
     
  14. ZJP

    ZJP Active Member Licensed User

    Works well with the "standard" engine and the other voice.

    JP
     
  15. Cor

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

    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?

    [​IMG]
     
  17. Erel

    Erel Administrator Staff 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.
     
  18. nfordbscndrd

    nfordbscndrd Well-Known Member Licensed User

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

    ZJP Active Member Licensed User

    Hi,

    Svox TTS Engine works well with Android 2.2 :sign0060:

    JP
     
  20. schimanski

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