Add text to speech feature

This commit is contained in:
c00lerxo 2019-01-24 14:44:02 +01:00
parent 1e9b9835c0
commit 31ffbb8f90
2 changed files with 36 additions and 17 deletions

View File

@ -29,7 +29,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -2,34 +2,24 @@ package com.example.se.catornot2;
import android.content.Intent; import android.content.Intent;
import android.speech.RecognizerIntent; import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import java.util.List; import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final int SPEECH_REQUEST_CODE = 0; private static final int SPEECH_REQUEST_CODE = 0;
TextToSpeech tts;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
} initializeTextToSpeech();
// Fires, when speechButton is clicked
public void handleSpeechButtonClick(View target) {
displaySpeechRecognizer();
}
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
"en-US");
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
} }
// This callback is invoked when the Speech Recognizer returns. // This callback is invoked when the Speech Recognizer returns.
@ -46,11 +36,40 @@ public class MainActivity extends AppCompatActivity {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
private void initializeTextToSpeech() {
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
}
// Fires, when speechButton is clicked
public void handleSpeechButtonClick(View target) {
displaySpeechRecognizer();
}
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
"en-US");
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
// Process voice command given by user
private void processVoiceCommand(String spokenText) { private void processVoiceCommand(String spokenText) {
if (spokenText.equals("take photo") || spokenText.equals("photo")) { if (spokenText.contains("photo") || spokenText.contains("foto")) {
// sound of taking photo // sound of taking photo
// ...
// say the result // say the result
System.out.println("Good!"); String toSpeak = "Lets do this, motherfucker";
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
} }
} }
} }