Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android Text To Speech example

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-23-2010, 08:36 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Text To Speech example

Android can synthesize and play text.
Using the TTS library you can easily add this feature to your application.



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
File Type: zip TTSExample.zip (6.2 KB, 1062 views)
File Type: apk TTS.apk (69.8 KB, 603 views)
Reply With Quote
  #2 (permalink)  
Old 12-23-2010, 08:44 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The library can be downloaded here: http://www.basic4ppc.com/forum/addit...html#post40481
Reply With Quote
  #3 (permalink)  
Old 12-23-2010, 08:54 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

I assume my device (ZTE-Blade/Orange San Francisco) must be missing something as all I get are "Language data not found" toasts.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #4 (permalink)  
Old 12-23-2010, 09:00 AM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

Works great

Going to use this for my guitar program

thanks

now only missing the jet midi library

grCor
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com
Reply With Quote
  #5 (permalink)  
Old 12-23-2010, 09:04 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

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!
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.

Last edited by agraham : 12-23-2010 at 09:30 AM.
Reply With Quote
  #6 (permalink)  
Old 12-23-2010, 09:13 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

"1 2 3 4 5 6 7 8 9 10"
Time to learn counting in other languages now...
Reply With Quote
  #7 (permalink)  
Old 12-23-2010, 09:38 AM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

I assume speech recognition is not far away......
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com
Reply With Quote
  #8 (permalink)  
Old 12-23-2010, 10:00 AM
Cor Cor is offline
Basic4ppc Veteran
 
Join Date: May 2007
Posts: 481
Default

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
__________________
Best regards,
Cor de Visser, Netherlands, HTC Magic 2.2.1

Amazing Guitar Chords for Android
http://android.ready4music.com

Last edited by Cor : 12-23-2010 at 11:20 AM.
Reply With Quote
  #9 (permalink)  
Old 12-23-2010, 11:07 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

Just a silly question: I got TTS for Vietnamese written in C#. Is there a chance to use it on Android?
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #10 (permalink)  
Old 12-23-2010, 01:01 PM
ZJP's Avatar
ZJP ZJP is offline
Senior Member
 
Join Date: Dec 2010
Posts: 136
Default

@Erel Thx

JP

Last edited by ZJP : 12-23-2010 at 01:12 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
wish:text to speech Cor Bugs & wishlist 2 12-23-2010 09:18 AM
Speech library for the DEVICE! agraham Additional Libraries 44 02-05-2010 11:33 AM
Desktop Text to Speech library agraham Additional Libraries 1 02-03-2009 08:12 PM
Text To Speech library (desktop only) Louis Additional Libraries 13 02-03-2009 09:19 AM
Speech recognition nm7s9 Questions (Windows Mobile) 1 07-30-2008 06:32 PM


All times are GMT. The time now is 10:27 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0