From 31ffbb8f908606251850a86cd1e7299b0e5a9ec3 Mon Sep 17 00:00:00 2001 From: c00lerxo Date: Thu, 24 Jan 2019 14:44:02 +0100 Subject: [PATCH] Add text to speech feature --- .idea/misc.xml | 2 +- .../example/se/catornot2/MainActivity.java | 51 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index b0c7b20..e0d5b93 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/java/com/example/se/catornot2/MainActivity.java b/app/src/main/java/com/example/se/catornot2/MainActivity.java index e986535..ba4e63a 100644 --- a/app/src/main/java/com/example/se/catornot2/MainActivity.java +++ b/app/src/main/java/com/example/se/catornot2/MainActivity.java @@ -2,34 +2,24 @@ package com.example.se.catornot2; import android.content.Intent; import android.speech.RecognizerIntent; +import android.speech.tts.TextToSpeech; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.util.List; +import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final int SPEECH_REQUEST_CODE = 0; + TextToSpeech tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - } - - // 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); + initializeTextToSpeech(); } // This callback is invoked when the Speech Recognizer returns. @@ -46,11 +36,40 @@ public class MainActivity extends AppCompatActivity { 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) { - if (spokenText.equals("take photo") || spokenText.equals("photo")) { + if (spokenText.contains("photo") || spokenText.contains("foto")) { // sound of taking photo + // ... + // say the result - System.out.println("Good!"); + String toSpeak = "Lets do this, motherfucker"; + tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); } } }