Site icon C1CTech

Android Speech to Text Example using SpeechRecognizer

&NewLine;<p>This post is about how to implement speech to text feature in an android application using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>SpeechRecognizer<&sol;strong><&sol;span>&period; <span style&equals;"color&colon; &num;008000&semi;"><strong>Speech to text<&sol;strong><&sol;span> means that anything that the user says is converted into text&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;040b6d&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;SpeechToTextDemo"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"qSVQW0S36Ek" title&equals;"Android Speech to Text Example using SpeechRecognizer"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;qSVQW0S36Ek"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;qSVQW0S36Ek&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Speech to Text Example using SpeechRecognizer"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>SpeechRecognizer<&sol;strong><&sol;span><&sol;h4> &NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">SpeechRecognizer<&sol;span><&sol;strong> class provides access to the speech recognition service&period; This service allows access to the speech recognizer&period; <br &sol;>This class&&num;8217&semi;s methods must be invoked only from the main application thread&period;<&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new Project<&sol;strong><&sol;span><&sol;h4> &NewLine;<p class&equals;"p1">1&period; Create a new project by going to <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;<p>2&period; Open AndroidManifest&period;xml file and add <strong><span style&equals;"color&colon; &num;0000ff&semi;">RECORD&lowbar;AUDIO<&sol;span><&sol;strong> permission as shown below&colon;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> package&equals;"com&period;c1ctech&period;speechtotextdemo"&gt&semi;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;RECORD&lowbar;AUDIO" &sol;&gt&semi;<&sol;span><&sol;strong><br &sol;> <br &sol;> &lt&semi;application<br &sol;> android&colon;allowBackup&equals;"true"<br &sol;> android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"<br &sol;> android&colon;label&equals;"&commat;string&sol;app&lowbar;name"<br &sol;> android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"<br &sol;> android&colon;supportsRtl&equals;"true"<br &sol;> android&colon;theme&equals;"&commat;style&sol;Theme&period;SpeechToTextDemo"&gt&semi;<br &sol;> &lt&semi;activity<br &sol;> android&colon;name&equals;"&period;MainActivity"<br &sol;> android&colon;exported&equals;"true"&gt&semi;<br &sol;> &lt&semi;intent-filter&gt&semi;<br &sol;> &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;<br &sol;> &lt&semi;&sol;intent-filter&gt&semi;<br &sol;> &lt&semi;&sol;activity&gt&semi;<br &sol;> &lt&semi;&sol;application&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;manifest&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The Layout File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>3&period; The below layout file consist of an ImageButton for the mic icon and an EditText to show the text that is converted from the speech&period;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;speech&lowbar;to&lowbar;text"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;alignParentTop&equals;"true"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"160dp"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;text&equals;"Speech to Text"<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;700"<br &sol;> android&colon;textSize&equals;"30sp"<br &sol;> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;RelativeLayout<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;rl"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;tv&lowbar;speech&lowbar;to&lowbar;text"<br &sol;> android&colon;layout&lowbar;marginLeft&equals;"10dp"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;layout&lowbar;marginRight&equals;"10dp"&gt&semi;<br &sol;><br &sol;> &lt&semi;EditText<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;edtSpeechText"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;centerInParent&equals;"true"<br &sol;> android&colon;layout&lowbar;marginRight&equals;"15dp"<br &sol;> android&colon;layout&lowbar;toLeftOf&equals;"&commat;id&sol;imgBtnMic"<br &sol;> android&colon;hint&equals;"Text output of recorded audio"<br &sol;> android&colon;padding&equals;"10dp" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;ImageButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;imgBtnMic"<br &sol;> android&colon;layout&lowbar;width&equals;"40dp"<br &sol;> android&colon;layout&lowbar;height&equals;"40dp"<br &sol;> android&colon;layout&lowbar;alignParentRight&equals;"true"<br &sol;> android&colon;backgroundTint&equals;"&num;F9F8FA"<br &sol;> android&colon;paddingRight&equals;"10dp"<br &sol;> android&colon;src&equals;"&commat;drawable&sol;ic&lowbar;mic&lowbar;off" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;&sol;RelativeLayout&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Requesting Permission at Runtime<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>4&period; From Android Marshmallow&comma; we must have to take <strong><span style&equals;"color&colon; &num;008000&semi;">RECORD&lowbar;AUDIO<&sol;span><&sol;strong> permission from the user at runtime&period;<&sol;p> &NewLine;<pre>if&lpar;isPermissionGranted&lpar;&rpar;&rpar;<br &sol;>&lbrace; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;request permission if it is not granted by user&period;<&sol;span><&sol;strong><br &sol;> requestPermission&lpar;&rpar;&semi;<br &sol;>&rcub;<br &sol;><br &sol;>private void requestPermission&lpar;&rpar; &lbrace;<br &sol;> if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;<br &sol;> ActivityCompat&period;requestPermissions&lpar;this&comma; new String&lbrack;&rsqb;&lbrace;Manifest&period;permission&period;RECORD&lowbar;AUDIO&rcub;&comma; PERMISSION&lowbar;RECORD&lowbar;AUDIO&lowbar;REQUEST&rpar;&semi;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>private boolean isPermissionGranted&lpar;&rpar; &lbrace;<br &sol;> return ContextCompat&period;checkSelfPermission&lpar;this&comma; Manifest&period;permission&period;RECORD&lowbar;AUDIO&rpar; &excl;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Override<br &sol;>public void onRequestPermissionsResult&lpar;int requestCode&comma; &commat;NonNull String&lbrack;&rsqb; permissions&comma; &commat;NonNull int&lbrack;&rsqb; grantResults&rpar; &lbrace;<br &sol;> super&period;onRequestPermissionsResult&lpar;requestCode&comma; permissions&comma; grantResults&rpar;&semi;<br &sol;> if &lpar;requestCode &equals;&equals; PERMISSION&lowbar;RECORD&lowbar;AUDIO&lowbar;REQUEST &amp&semi;&amp&semi; grantResults&period;length &gt&semi; 0&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;when user allows the permission&period;<&sol;span><&sol;strong><br &sol;> if &lpar;grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&rpar;<br &sol;> Toast&period;makeText&lpar;this&comma; "Permission Granted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating a SpeechRecognizer <&sol;span><&sol;strong><&sol;h4> &NewLine;<p>5&period; To implement speech to text functionality&comma; we need to create a <strong><span style&equals;"color&colon; &num;008000&semi;">SpeechRecognizer<&sol;span><&sol;strong> instance&period; We also need an Intent to listen to the speech&period;<&sol;p> &NewLine;<pre>private SpeechRecognizer speechRecognizer&semi;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating SpeechRecognizer instance<&sol;span><&sol;strong><br &sol;>speechRecognizer &equals; SpeechRecognizer&period;createSpeechRecognizer&lpar;this&rpar;&semi;<br &sol;><br &sol;>final Intent speechRecognizerIntent &equals; new Intent&lpar;RecognizerIntent&period;ACTION&lowbar;RECOGNIZE&lowbar;SPEECH&rpar;&semi;<br &sol;>speechRecognizerIntent&period;putExtra&lpar;RecognizerIntent&period;EXTRA&lowbar;LANGUAGE&lowbar;MODEL&comma; RecognizerIntent&period;LANGUAGE&lowbar;MODEL&lowbar;FREE&lowbar;FORM&rpar;&semi;<br &sol;>speechRecognizerIntent&period;putExtra&lpar;RecognizerIntent&period;EXTRA&lowbar;LANGUAGE&comma; Locale&period;getDefault&lpar;&rpar;&rpar;&semi;<&sol;pre> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Setting RecognitionListener to SpeechRecognizer<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>6<strong>&period; <&sol;strong>The <span style&equals;"color&colon; &num;008000&semi;"><strong>setRecognitionListener&lpar;&rpar;<&sol;strong><&sol;span>  will receive all the callbacks from the created <strong><span style&equals;"color&colon; &num;008000&semi;">SpeechRecognizer<&sol;span><&sol;strong>&period;<&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;<pre>speechRecognizer&period;setRecognitionListener&lpar;new RecognitionListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onReadyForSpeech&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;The user has started to speak&period;<&sol;span><&sol;strong><br &sol;> &commat;Override<br &sol;> public void onBeginningOfSpeech&lpar;&rpar; &lbrace;<br &sol;> editText&period;setText&lpar;""&rpar;&semi;<br &sol;> editText&period;setHint&lpar;"Listening&period;&period;&period;"&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onRmsChanged&lpar;float v&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onBufferReceived&lpar;byte&lbrack;&rsqb; bytes&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onEndOfSpeech&lpar;&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onError&lpar;int i&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;called when recognition results are ready&period;<&sol;strong><&sol;span><br &sol;> &commat;Override<br &sol;> public void onResults&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> micButton&period;setImageResource&lpar;R&period;drawable&period;ic&lowbar;mic&lowbar;off&rpar;&semi;<br &sol;> ArrayList&lt&semi;String&gt&semi; data &equals; bundle&period;getStringArrayList&lpar;SpeechRecognizer&period;RESULTS&lowbar;RECOGNITION&rpar;&semi;<br &sol;> editText&period;setText&lpar;data&period;get&lpar;0&rpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onPartialResults&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onEvent&lpar;int i&comma; Bundle bundle&rpar; &lbrace;<br &sol;> &rcub;<br &sol;>&rcub;&rpar;&semi;<&sol;pre> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Adding Touch Listener to Button<&sol;span><&sol;strong><&sol;h4> &NewLine;<ul> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DOWN &lpar;Button pressed gesture has started&rpar;&colon;<&sol;span><&sol;strong> starts listening for speech&period;<&sol;li> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;UP &lpar;Button pressed gesture has finished&rpar;&colon;<&sol;span><&sol;strong> stops listening for speech&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>micButton&period;setOnTouchListener&lpar;new View&period;OnTouchListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public boolean onTouch&lpar;View view&comma; MotionEvent motionEvent&rpar; &lbrace;<br &sol;><br &sol;> if &lpar;motionEvent&period;getAction&lpar;&rpar; &equals;&equals; MotionEvent&period;ACTION&lowbar;UP&rpar; &lbrace;<br &sol;> speechRecognizer&period;stopListening&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> if &lpar;motionEvent&period;getAction&lpar;&rpar; &equals;&equals; MotionEvent&period;ACTION&lowbar;DOWN&rpar; &lbrace;<br &sol;> micButton&period;setImageResource&lpar;R&period;drawable&period;ic&lowbar;mic&rpar;&semi;<br &sol;> speechRecognizer&period;startListening&lpar;speechRecognizerIntent&rpar;&semi;<br &sol;> &rcub;<br &sol;> return false&semi;<br &sol;> &rcub;<br &sol;>&rcub;&rpar;&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Complete MainActivity Code<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>7&period; Given below is the complete MainActivity code&period;<&sol;p> &NewLine;<pre>package com&period;c1ctech&period;speechtotextdemo&semi;<br &sol;><br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;<br &sol;>import android&period;os&period;Bundle&semi;<br &sol;>import androidx&period;annotation&period;NonNull&semi;<br &sol;>import androidx&period;core&period;app&period;ActivityCompat&semi;<br &sol;>import androidx&period;core&period;content&period;ContextCompat&semi;<br &sol;>import android&period;Manifest&semi;<br &sol;>import android&period;content&period;Intent&semi;<br &sol;>import android&period;content&period;pm&period;PackageManager&semi;<br &sol;>import android&period;os&period;Build&semi;<br &sol;>import android&period;speech&period;RecognitionListener&semi;<br &sol;>import android&period;speech&period;RecognizerIntent&semi;<br &sol;>import android&period;speech&period;SpeechRecognizer&semi;<br &sol;>import android&period;view&period;MotionEvent&semi;<br &sol;>import android&period;view&period;View&semi;<br &sol;>import android&period;widget&period;&ast;&semi;<br &sol;>import android&period;widget&period;Toast&semi;<br &sol;>import java&period;util&period;ArrayList&semi;<br &sol;>import java&period;util&period;Locale&semi;<br &sol;><br &sol;>public class MainActivity extends AppCompatActivity &lbrace;<br &sol;><br &sol;> public static final Integer PERMISSION&lowbar;RECORD&lowbar;AUDIO&lowbar;REQUEST &equals; 1&semi;<br &sol;> private SpeechRecognizer speechRecognizer&semi;<br &sol;> private EditText editText&semi;<br &sol;> private ImageButton micButton&semi;<br &sol;><br &sol;> &commat;Override<br &sol;> protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;&semi;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;<br &sol;><br &sol;> if &lpar;isPermissionGranted&lpar;&rpar;&rpar; &lbrace;<br &sol;> requestPermission&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> editText &equals; findViewById&lpar;R&period;id&period;edtSpeechText&rpar;&semi;<br &sol;> micButton &equals; findViewById&lpar;R&period;id&period;imgBtnMic&rpar;&semi;<br &sol;> speechRecognizer &equals; SpeechRecognizer&period;createSpeechRecognizer&lpar;this&rpar;&semi;<br &sol;><br &sol;> final Intent speechRecognizerIntent &equals; new Intent&lpar;RecognizerIntent&period;ACTION&lowbar;RECOGNIZE&lowbar;SPEECH&rpar;&semi;<br &sol;> speechRecognizerIntent&period;putExtra&lpar;RecognizerIntent&period;EXTRA&lowbar;LANGUAGE&lowbar;MODEL&comma; RecognizerIntent&period;LANGUAGE&lowbar;MODEL&lowbar;FREE&lowbar;FORM&rpar;&semi;<br &sol;> speechRecognizerIntent&period;putExtra&lpar;RecognizerIntent&period;EXTRA&lowbar;LANGUAGE&comma; Locale&period;getDefault&lpar;&rpar;&rpar;&semi;<br &sol;><br &sol;> speechRecognizer&period;setRecognitionListener&lpar;new RecognitionListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public void onReadyForSpeech&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onBeginningOfSpeech&lpar;&rpar; &lbrace;<br &sol;> editText&period;setText&lpar;""&rpar;&semi;<br &sol;> editText&period;setHint&lpar;"Listening&period;&period;&period;"&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onRmsChanged&lpar;float v&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onBufferReceived&lpar;byte&lbrack;&rsqb; bytes&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onEndOfSpeech&lpar;&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onError&lpar;int i&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onResults&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> micButton&period;setImageResource&lpar;R&period;drawable&period;ic&lowbar;mic&lowbar;off&rpar;&semi;<br &sol;> ArrayList&lt&semi;String&gt&semi; data &equals; bundle&period;getStringArrayList&lpar;SpeechRecognizer&period;RESULTS&lowbar;RECOGNITION&rpar;&semi;<br &sol;> editText&period;setText&lpar;data&period;get&lpar;0&rpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onPartialResults&lpar;Bundle bundle&rpar; &lbrace;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onEvent&lpar;int i&comma; Bundle bundle&rpar; &lbrace;<br &sol;><br &sol;> &rcub;<br &sol;> &rcub;&rpar;&semi;<br &sol;><br &sol;> micButton&period;setOnTouchListener&lpar;new View&period;OnTouchListener&lpar;&rpar; &lbrace;<br &sol;> &commat;Override<br &sol;> public boolean onTouch&lpar;View view&comma; MotionEvent motionEvent&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;ACTION&lowbar;UP&colon; A pressed gesture has finished&period;<&sol;span><&sol;strong><br &sol;> if &lpar;motionEvent&period;getAction&lpar;&rpar; &equals;&equals; MotionEvent&period;ACTION&lowbar;UP&rpar; &lbrace;<br &sol;> speechRecognizer&period;stopListening&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;ACTION&lowbar;DOWN&colon; A pressed gesture has started&period;<&sol;strong><&sol;span><br &sol;> if &lpar;motionEvent&period;getAction&lpar;&rpar; &equals;&equals; MotionEvent&period;ACTION&lowbar;DOWN&rpar; &lbrace;<br &sol;> micButton&period;setImageResource&lpar;R&period;drawable&period;ic&lowbar;mic&rpar;&semi;<br &sol;> speechRecognizer&period;startListening&lpar;speechRecognizerIntent&rpar;&semi;<br &sol;> &rcub;<br &sol;> return false&semi;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> protected void onDestroy&lpar;&rpar; &lbrace;<br &sol;> super&period;onDestroy&lpar;&rpar;&semi;<br &sol;> speechRecognizer&period;destroy&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> private void requestPermission&lpar;&rpar; &lbrace;<br &sol;> if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;M&rpar; &lbrace;<br &sol;> ActivityCompat&period;requestPermissions&lpar;this&comma; new String&lbrack;&rsqb;&lbrace;Manifest&period;permission&period;RECORD&lowbar;AUDIO&rcub;&comma; PERMISSION&lowbar;RECORD&lowbar;AUDIO&lowbar;REQUEST&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> private boolean isPermissionGranted&lpar;&rpar; &lbrace;<br &sol;> return ContextCompat&period;checkSelfPermission&lpar;this&comma; Manifest&period;permission&period;RECORD&lowbar;AUDIO&rpar; &excl;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onRequestPermissionsResult&lpar;int requestCode&comma; &commat;NonNull String&lbrack;&rsqb; permissions&comma; &commat;NonNull int&lbrack;&rsqb; grantResults&rpar; &lbrace;<br &sol;> super&period;onRequestPermissionsResult&lpar;requestCode&comma; permissions&comma; grantResults&rpar;&semi;<br &sol;> if &lpar;requestCode &equals;&equals; PERMISSION&lowbar;RECORD&lowbar;AUDIO&lowbar;REQUEST &amp&semi;&amp&semi; grantResults&period;length &gt&semi; 0&rpar; &lbrace;<br &sol;><br &sol;> if &lpar;grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&rpar;<br &sol;> Toast&period;makeText&lpar;this&comma; "Permission Granted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>When you run the app it will look like this as shown below&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2971" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;12&sol;Screenshot&lowbar;20211201-195838&lowbar;Permission-controller-498x1024&period;jpg" alt&equals;"" width&equals;"312" height&equals;"642" &sol;>    <img class&equals;"alignnone wp-image-2972" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;12&sol;Screenshot&lowbar;20211202-114237&lowbar;SpeechToTextDemo-498x1024&period;jpg" alt&equals;"" width&equals;"312" height&equals;"641" &sol;>&NewLine;

Exit mobile version