Sie sind auf Seite 1von 6

Android Text To Speech Tutorial

Android allows you convert your text into voice. Not only you can
convert it but it also allows you to speak text in variety of different
languages.
Android provides TextToSpeech class for this purpose. In order to
use this class, you need to instantiate an object of this class and also
specify the initistnere. Its syntax is given below!
private "ditText write#ttobj$new
TextToSpeech%getApplication&ontext%', new
TextToSpeech.(nInitistener%' ) *(verride public void onInit%int
status' ) ++'#
In this listener , you have to specify the properties for TextToSpeech
object , such as its language ,pitch e.t.c. anguage can be set by
calling setanguage%' ,ethod. Its syntax is given below!
ttobj.setanguage%ocale.-.'#
The ,ethod setanguage takes an ocale object as para,eter. The
list of so,e of the locales availaible are given below!
Sr.No ocale
/ -S
0 &ANA1A234"N&5
6 7"48AN9
: ITA9
; <A=AN
> &5INA
(nce you have set the language, you can call speak ,ethod of the
class to speak the text. Its syntax is given below!
ttobj.speak%toSpeak, TextToSpeech.?-"-"23-S5, null'#
Apart fro, the speak ,ethod, there are so,e other ,ethods availaible
in the TextToSpeech class. They are listed below!
Sr.No 8ethod @ description
/
addSpeech%String text, String filena,e'
This ,ethod adds a ,apping between a string of text and a sound file.
0
getanguage%'
This ,ethod returns a ocale instance describing the language.
6
isSpeaking%'
This ,ethod checks whether the TextToSpeech engine is busy speaking.
:
set=itch%float pitch'
This ,ethod sets the speech pitch for the TextToSpeech engine.
;
setSpeech4ate%float speech4ate'
This ,ethod sets the speech rate.
>
shutdown%'
This ,ethod releases the resources used by the TextToSpeech engine.
A
stop%'
This ,ethod stop the speak.
"xa,ple
The below exa,ple de,onstrates the use of TextToSpeech class. It
crates a basic application that allows you to set write text and speak
it.
To experi,ent with this exa,ple , you need to run this on an actual
device.
Steps 1escription
/
9ou will use "clipse I1" to create an Android application and na,e it as
TextToSpeech under a package co,.exa,ple.texttospeech. Bhile
creating this project, ,ake sure you Target S1. and &o,pile Bith at
the latest version of Android S1. to use higher levels of A=Is.
0 8odify srcC8ainActivity.java file to add TextToSpeech code.
6
8odify layout D8 file resClayoutCactivity2,ain.x,l add any 7-I
co,ponent if reEuired.
: 8odify resCvaluesCstring.x,l file and add necessary string co,ponents.
;
4un the application and choose a running android device and install the
application on it and verify the results.
5ere is the content of
srcCco,.exa,ple.texttospeechC8ainActivity.java.
package co,.exa,ple.texttospeech#
i,port java.util.ocale#i,port java.util.4ando,#
i,port android.app.Activity#i,port android.os.Fundle#i,port
android.speech.tts.TextToSpeech#i,port android.view.8enu#i,port
android.view.Giew#i,port android.widget."ditText#i,port
android.widget.Toast#
public class 8ainActivity extends Activity )
TextToSpeech ttobj# private "ditText write# *(verride
protected void on&reate%Fundle savedInstanceState'
) super.on&reate%savedInstanceState'#
set&ontentGiew%4.layout.activity2,ain'# write $
%"ditText'findGiewFyId%4.id.editText/'# ttobj$new
TextToSpeech%getApplication&ontext%',
new TextToSpeech.(nInitistener%' ) *(verride public
void onInit%int status' ) if%status H$ TextToSpeech."44(4')
ttobj.setanguage%ocale.-.'# +
+ +'# + *(verride public void on=ause%'
) if%ttobj H$null') ttobj.stop%'# ttobj.shutdown%'#
+ super.on=ause%'# + *(verride public boolean
on&reate(ptions8enu%8enu ,enu' ) CC Inflate the ,enu# this adds
ite,s to the action bar if it is present.
get8enuInflater%'.inflate%4.,enu.,ain, ,enu'# return true# +
public void speakText%Giew view') String toSpeak $
write.getText%'.toString%'#
Toast.,akeText%getApplication&ontext%', toSpeak,
Toast."N7T52S5(4T'.show%'# ttobj.speak%toSpeak,
TextToSpeech.?-"-"23-S5, null'#
++
5ere is the content of activity2,ain.x,l
I4elativeayout
x,lns!android$Jhttp!CCsche,as.android.co,CapkCresCandroidJ
x,lns!tools$Jhttp!CCsche,as.android.co,CtoolsJ
android!layout2width$J,atch2parentJ
android!layout2height$J,atch2parentJ
android!paddingFotto,$J*di,enCactivity2vertical2,arginJ
android!paddingeft$J*di,enCactivity2horiKontal2,arginJ
android!padding4ight$J*di,enCactivity2horiKontal2,arginJ
android!paddingTop$J*di,enCactivity2vertical2,arginJ
tools!context$J.8ainActivityJ L
IFutton android!id$J*MidCbutton/J
android!layout2width$Jwrap2contentJ
android!layout2height$Jwrap2contentJ
android!layout2align=arentFotto,$JtrueJ
android!layout2align=arent4ight$JtrueJ
android!layout2,arginFotto,$J/NNdpJ
android!layout2,argin4ight$J>AdpJ android!on&lick$JspeakTextJ
android!text$J*stringCtext/J CL
I"ditText android!id$J*MidCeditText/J
android!layout2width$Jwrap2contentJ
android!layout2height$Jwrap2contentJ
android!layout2above$J*MidCbutton/J
android!layout2center5oriKontal$JtrueJ
android!layout2,arginFotto,$JN/dpJ android!e,s$J/OJ L
IreEuest3ocus CL IC"ditTextL
ITextGiew android!id$J*MidCtextGiew/J
android!layout2width$Jwrap2contentJ
android!layout2height$Jwrap2contentJ
android!layout2align=arentTop$JtrueJ
android!layout2center5oriKontal$JtrueJ
android!layout2,arginTop$J0OdpJ android!text$J*stringCwriteJ
android!textAppearance$JPandroid!attrCtextAppearanceargeJ
CLIC4elativeayoutL
5ere is the content of Strings.x,l.
IPx,l version$J/.OJ encoding$JutfQNJPLIresourcesL Istring
na,e$Japp2na,eJLTextToSpeechICstringL Istring
na,e$Jaction2settingsJLSettingsICstringL Istring
na,e$Jhello2worldJL5ello worldHICstringL Istring
na,e$Jtext/JLText to SpeechICstringL Istring
na,e$JwriteJLBrite TextICstringLICresourcesL
5ere is the content of Android8anifest.x,l
IPx,l version$J/.OJ encoding$JutfQNJPLI,anifest
x,lns!android$Jhttp!CCsche,as.android.co,CapkCresCandroidJ
package$Jco,.exa,ple.texttospeechJ android!version&ode$J/J
android!versionNa,e$J/.OJ L
IusesQsdk android!,inSdkGersion$JNJ
android!targetSdkGersion$J/AJ CL
Iapplication android!allowFackup$JtrueJ
android!icon$J*drawableCic2launcherJ
android!label$J*stringCapp2na,eJ
android!the,e$J*styleCAppThe,eJ L Iactivity
android!na,e$Jco,.exa,ple.texttospeech.8ainActivityJ
android!label$J*stringCapp2na,eJ L IintentQfilterL
Iaction android!na,e$Jandroid.intent.action.8AINJ CL
Icategory
android!na,e$Jandroid.intent.category.A-N&5"4J CL ICintentQ
filterL ICactivityL ICapplicationLIC,anifestL
etRs try to run your TextToSpeech application. I
assu,e you have connected your actual Android 8obile device with
your co,puter. To run the app fro, "clipse, open one of your
projectRs activity files and click 4un icon fro, the toolbar. Fefore
starting your application, "clipse will display following window to select
an option where you
want to run your
Android
application.

Select your ,obile
device as an option
and then check your
,obile device which
will display following
screen.

Now just type so,e text in the field and click on the text to speech
button below. A notification would appear and text will be spoken. It is
shown in the i,age below!

Now type so,ething
else and repeat the
step again with
different locale. 9ou
will again hear sound.
This is shown below!

Das könnte Ihnen auch gefallen