Friday 12 September 2014

HB Blog 19: Sample code for converting "Speech" to "Text" and vice-versa.

       Speech technology allows hands free use of the mobile.Android APIs provide us a large number of packages which includes class and methods that makes easy to convert "Speech" to "Text" and vice-versa.I have shown a sample of both speech to text as well as text to speech conversion.

Refer the below link for complete sample code(Speech To Text):-
Download Sample Code
Download Apk File
Refer the below link for complete sample code(Text To Speech):-
Download Sample Code
Download Apk File

Speech To Text
 
Have a look on few code snippets,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//Just,fire an intent to start the speech recognition activity.
//This Starts an activity that will prompt the user for speech and send it through //a speech recognizer. 
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
startActivityForResult(intent, RESULT_SPEECH);
//get the result using onActivityResult and set to required text view
//you can also provide pending intent if one is provided.
ArrayList<String> text = data.
getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textView.setText(text.get(0));
Text To Speech
 
Have a look on few code snippets,
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//Use OnInitListener of to signal the completion of the TextToSpeech engine initialization  
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener(){
            @Override
            public void onInit(int status){
                if(status != TextToSpeech.ERROR){
                    textToSpeech.setLanguage(Locale.UK);
                }
            }
        });
//Then use "speak" method that speaks the string using the specified queuing strategy and speech parameters.
public void speakText(View view){
        String toSpeak = et_txt.getText().toString();
        textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
    }

No comments:

Post a Comment