SpeechRecognizer cancel on error
This commit is contained in:
parent
129af39120
commit
2b985a4a29
|
@ -1,6 +1,6 @@
|
|||
package com.example.yink.amadeus;
|
||||
|
||||
/**
|
||||
/*
|
||||
* Big thanks to https://github.com/RIP95 aka Emojikage
|
||||
*/
|
||||
|
||||
|
@ -36,18 +36,20 @@ public class MainActivity extends AppCompatActivity {
|
|||
ImageView kurisu;
|
||||
AnimationDrawable animation;
|
||||
Handler handler;
|
||||
Handler speechHandler;
|
||||
Boolean isLoop = false;
|
||||
Boolean isSpeaking = false;
|
||||
Boolean isRecognizing = false;
|
||||
ArrayList<VoiceLine> voiceLines = new ArrayList<>();
|
||||
private SpeechRecognizer sr;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
kurisu = (ImageView) findViewById(R.id.imageView_kurisu);
|
||||
handler = new Handler();
|
||||
speechHandler = new Handler();
|
||||
setupLines();
|
||||
speak(voiceLines.get(0));
|
||||
|
||||
|
@ -77,17 +79,19 @@ public class MainActivity extends AppCompatActivity {
|
|||
int permissionCheck = ContextCompat.checkSelfPermission(host,
|
||||
Manifest.permission.RECORD_AUDIO);
|
||||
|
||||
/* Input while loop producing bugs and mixes with output */
|
||||
/* Input while loop producing bugs and mixes with output */
|
||||
if (!isLoop && !isSpeaking) {
|
||||
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
|
||||
isRecognizing = true;
|
||||
promptSpeechInput();
|
||||
} else {
|
||||
speak(new VoiceLine(R.raw.daga_kotowaru, Mood.PISSED));
|
||||
}
|
||||
}
|
||||
|
||||
} else if (!isLoop && !isSpeaking) {
|
||||
isRecognizing = true;
|
||||
promptSpeechInput();
|
||||
}
|
||||
|
||||
}});
|
||||
|
||||
kurisu.setOnLongClickListener(new View.OnLongClickListener() {
|
||||
|
@ -129,33 +133,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
|
||||
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
|
||||
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ja-JP");
|
||||
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"com.example.yink.amadeus");
|
||||
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example.yink.amadeus");
|
||||
|
||||
/* TODO: Make this damn thing work without dialog. */
|
||||
//sr.startListening(intent);
|
||||
try {
|
||||
startActivityForResult(intent, 1);
|
||||
} catch (ActivityNotFoundException a) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
switch (requestCode) {
|
||||
case 1: {
|
||||
if (resultCode == RESULT_OK && null != data) {
|
||||
|
||||
ArrayList<String> input = data
|
||||
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
|
||||
answerSpeech(input.get(0));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
sr.startListening(intent);
|
||||
}
|
||||
|
||||
public void speak(VoiceLine line) {
|
||||
|
@ -349,6 +329,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
public void onError(int error) {
|
||||
Log.d(TAG, "error " + error);
|
||||
sr.cancel();
|
||||
}
|
||||
public void onResults(Bundle results) {
|
||||
String input = "";
|
||||
|
@ -365,5 +346,4 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue