In Android Studio, use features of speech recognition and text-to-speech feature
ID: 3734469 • Letter: I
Question
In Android Studio, use features of speech recognition and text-to-speech features in Android. The UI has already been designed by the pre-defined XML file.
To access the documents and APK file, please see the Google Drive: https://drive.google.com/open?id=1E-qTsqrSkYIWYt9CDdaR31AZmQlC0XaX
Layout - activity_main.xml (for your reference)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"android:layout_height="fill_parent" android:paddingLeft="10dp"
android:paddingRight="10dp" android:gravity="top" android:orientation="vertical"android:background="#000000">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginTop="20dp" android:textColor="#ffffff" android:textSize="16sp"android:text="@string/help" />
<Button
android:id="@+id/ButtonRecord" android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginTop="20dp"android:onClick="recordSpeech" android:text="@string/button_record" android:textColor="#ffffff"android:background="#4d4d4d" />
<TextView
android:id="@+id/TextSaid" android:layout_width="fill_parent" android:layout_height="wrap_content"android:gravity="center" android:lines="5" android:text="@string/recorded_text"android:textSize="18sp" android:textColor="#ffffff"/>
<Button
android:id="@+id/ButtonRead" android:layout_width="fill_parent"android:layout_height="wrap_content" android:enabled="false" android:onClick="readText"android:text="@string/button_read" android:textColor="#ffffff" android:background="#4d4d4d" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginTop="20dp" android:gravity="center_horizontal" android:textSize="16sp"android:textColor="#ffffff" android:text="@string/try_help"/>
<EditText android:id="@+id/editTextField" android:layout_width="fill_parent"android:layout_height="40dp" android:layout_marginTop="10dp"android:inputType="textCapSentences" android:background="#ffffff"/>
</LinearLayout>
To access the documents and APK file, please see the Google Drive: https://drive.google.com/open?id=1E-qTsqrSkYIWYt9CDdaR31AZmQlC0XaX
1. Problem 1 - Android Speech Recognition and Text-to-Speech (TTS) Please design a mobile app that uses Android speech recognition and text-to-speech (TTS) features. The UI layout contains two buttons ("Record Speech" and "Read Text Aloud"), three TextView and one EditView. The expected results are similar to Figure 1 to 5 87%0 9:24 PM 7.11 87%ù 9:25 PM TextView for displaying explanation Lab MyVoiceAssistant Lab_MyVoiceAssistant Press the Record Speech button to translate speech to text. Press the Read Text Aloud button to read text with Text-To-Speech (TTS) Press the Record Speech button to translate speech to text. Press the Read Text Aloud button to read text with Text-To-Speech (TTS) Button to trigger speech recognition RECORD SPEECH RECORD SPEECH Area to display text from speech computer science at Wentworth Institute of Technology (Recorded text will display here) READ TEXT ALOUD READ TEXT ALOUD Button to speak out (TTS) above sentence Try speech button on Android keyboard Try speech button on Android keyboard The Area to display text from speech Figure 1: Initial display Figure 2: Show the result of "Record Speech" Note: The "READ TEXT ALOUD" button is disabled by default.Explanation / Answer
i am providing you functionality for task which you want to perform . just adjust this according to your xml file . you want to convert speech into text .hence this code is of mainActivity file . after adding packages and required imports type this.:
public class MainActivity extends AppCompatActivity {
private TextView voiceInputbutton;
private TextView speakoutputButtont;
private final int REQ_CODE_SPEECH_INPUT = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceInputbutton = (TextView) findViewById(R.id.voiceInputbutton);
speakoutputButton = (TextView) findViewById(R.id.btnSpeak);
speakoutputButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askSpeechInput();
}
});
}
// Showing google speech input dialog
private void askSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Hi speak something");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
// Receiving speech input
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInputbutton.setText(result.get(0));
}
break;
}
}
}
}
in xml file just change the id name of two buttons used ,to voiceInputbutton & btnSpeak
i hope this will be helpful .