Site icon C1CTech

Android Text to Speech Example

&NewLine;<p class&equals;"p1">Android is providing a cool feature &lpar;from Android 1&period;6&rpar; called <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Text to Speech &lpar;TTS&rpar; or speech synthesis<&sol;strong><&sol;span> which speaks the text in different languages&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>This tutorial is about how to work with android text to speech or android speech synthesis&period; It also includes changing the language type&comma; speech pitch and speech rate&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button is-style-fill"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;TextToSpeechDemo" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"OEiHmipXEgw" title&equals;"Android TextToSpeech Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;OEiHmipXEgw"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;OEiHmipXEgw&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android TextToSpeech Example"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new Project<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><b>1<&sol;b>&period; Create a new project by going to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>File <&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;"> New Android Project<&sol;span>&comma;<&sol;b> fill the required details and then click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><b>2<&sol;b>&period; The below layout file consist of a textView &comma; an input field and a button to trigger a event that will take text from input field and speaks out&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;50dp"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;text&equals;"&commat;string&sol;text&lowbar;to&lowbar;speech"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textSize&lowbar;30sp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;EditText&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;edt&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginStart&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;30dp"&NewLine; android&colon;ems&equals;"10"&NewLine; android&colon;background&equals;"&commat;drawable&sol;background"&NewLine; android&colon;hint&equals;"&commat;string&sol;enter&lowbar;some&lowbar;text&lowbar;to&lowbar;speak"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;0"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;text&lowbar;to&lowbar;speech" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;speak"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;30dp"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;text&equals;"&commat;string&sol;speak&lowbar;text"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textSize&lowbar;18sp"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;edt&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;edt&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;edt&lowbar;text" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><div class&equals;"article-post-image"><strong>3<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span><span style&equals;"color&colon; &num;000000&semi;">&comma;<&sol;span><&sol;strong><&sol;span> implement it from <span style&equals;"color&colon; &num;0000ff&semi;"><strong>TextToSpeech&period;OnInitListener <&sol;strong><&sol;span>and add the below code<strong>&period;<&sol;strong><&sol;div><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;texttospeechdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;speech&period;tts&period;TextToSpeech&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;EditText&semi;&NewLine;&NewLine;import java&period;util&period;Locale&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity implements TextToSpeech&period;OnInitListener &lbrace;&NewLine;&NewLine; EditText edtText&semi;&NewLine; Button btnSpeak&semi;&NewLine; TextToSpeech tts&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;get views by id<&sol;span><&sol;strong>&NewLine; edtText &equals; findViewById&lpar;R&period;id&period;edt&lowbar;text&rpar;&semi;&NewLine; btnSpeak &equals; findViewById&lpar;R&period;id&period;btn&lowbar;speak&rpar;&semi;&NewLine;&NewLine; tts &equals; new TextToSpeech&lpar;this&comma; this&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; button on click event<&sol;span><&sol;strong>&NewLine; btnSpeak&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;get edt value<&sol;span><&sol;strong>&NewLine; String text &equals; edtText&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&semi;&NewLine;&NewLine; tts&period;speak&lpar;text&comma; TextToSpeech&period;QUEUE&lowbar;FLUSH&comma; null&comma; "abc"&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onInit&lpar;int status&rpar; &lbrace;&NewLine;&NewLine; if &lpar;status &equals;&equals; TextToSpeech&period;SUCCESS&rpar; &lbrace;&NewLine;&NewLine; int result &equals; tts&period;setLanguage&lpar;Locale&period;US&rpar;&semi;&NewLine;&NewLine; if &lpar;result &equals;&equals; TextToSpeech&period;LANG&lowbar;MISSING&lowbar;DATA&NewLine; &vert;&vert; result &equals;&equals; TextToSpeech&period;LANG&lowbar;NOT&lowbar;SUPPORTED&rpar; &lbrace;&NewLine; Log&period;e&lpar;"TTS"&comma; "This Language is not supported"&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub; else &lbrace;&NewLine; Log&period;e&lpar;"TTS"&comma; "Initilization Failed&excl;"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onDestroy&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;don't forget to shutdown tts<&sol;span><&sol;strong>&NewLine; if &lpar;tts &excl;&equals; null&rpar; &lbrace;&NewLine; tts&period;stop&lpar;&rpar;&semi;&NewLine; tts&period;shutdown&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; super&period;onDestroy&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>In the above code&comma;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onInit&lpar;&rpar;<&sol;strong><&sol;span>&colon; Called to signal the completion of the TextToSpeech engine initialization&period; Here&comma; you have to specify the properties for TextToSpeech object &comma; such as its language &comma;pitch e&period;t&period;c&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">speak&lpar;&rpar;<&sol;span><&sol;strong>&colon; Speaks the text using the specified queuing strategy and speech parameters&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">setLanguage&lpar;&rpar;<&sol;span><&sol;strong>&colon; Sets the text-to-speech language&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">stop&lpar;&rpar;<&sol;span><&sol;strong>&colon; stop the speak&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">shutdown&lpar;&rpar;<&sol;span><&sol;strong>&colon; releases the resources used by the TextToSpeech engine&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>4<&sol;strong>&period; Now run your project and test your app by entering some text in input filed&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;01&sol;Screenshot&lowbar;1611838103-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2346" width&equals;"381" height&equals;"678"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading">&nbsp&semi;<&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Changing Language<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">You can change language to speak by using&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><b>setLanguage&lpar;&rpar;<&sol;b><&sol;span> method&period; Lot of languages are supported like Canada&comma; French&comma; Chinese&comma; Germany etc&period;&comma;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">tts&period;setLanguage&lpar;Locale&period;CHINESE&rpar;&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Chinese language<&sol;span><&sol;strong>&NewLine;tts&period;setLanguage&lpar;Locale&period;FRENCH&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; French language<&sol;strong><&sol;span>&NewLine;tts&period;setLanguage&lpar;Locale&period;GERMAN&rpar;&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; German language<&sol;span><&sol;strong><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Changing Pitch Rate<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">You can set speech pitch for the TextToSpeech engine by using <span style&equals;"color&colon; &num;0000ff&semi;"><b>setPitch&lpar;&rpar;<&sol;b>&nbsp&semi;<&sol;span>function&period; By default the value is&nbsp&semi;<b>1&period;0<&sol;b>&nbsp&semi;You can set lower values than&nbsp&semi;<b>1&period;0<&sol;b>&nbsp&semi;to decrease pitch level or greater values for increase pitch level&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">tts&period;setPitch&lpar;0&period;6f&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Changing Speech Rate<&sol;span><&sol;strong><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">The speech rate can be set using <span style&equals;"color&colon; &num;0000ff&semi;"><b>setSpeechRate&lpar;&rpar;<&sol;b><&sol;span>&period; It also takes default of&nbsp&semi;<b>1&period;0<&sol;b> value&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">tts&period;setSpeechRate&lpar;2f&rpar;&semi;<&sol;pre>&NewLine;&NewLine;

Exit mobile version