Add speech recognition feature and mockup UI

This commit is contained in:
c00lerxo 2019-01-24 13:17:44 +01:00
parent 3992087bcd
commit 1e9b9835c0
5 changed files with 60 additions and 8 deletions

View File

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

View File

@ -2,6 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.se.catornot2">
<!-- Permission for speech recognizer -->
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -9,6 +17,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -1,13 +1,56 @@
package com.example.se.catornot2;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int SPEECH_REQUEST_CODE = 0;
@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);
}
// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
processVoiceCommand(spokenText);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void processVoiceCommand(String spokenText) {
if (spokenText.equals("take photo") || spokenText.equals("photo")) {
// sound of taking photo
// say the result
System.out.println("Good!");
}
}
}

View File

@ -6,13 +6,12 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
<Button
android:id="@+id/speechButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:onClick="handleSpeechButtonClick"
android:text="@string/speech_button"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="230dp" />
</android.support.constraint.ConstraintLayout>

View File

@ -1,3 +1,4 @@
<resources>
<string name="app_name">Cat or Not 2</string>
<string name="speech_button">Click me!</string>
</resources>