Android TextToSpeech behaves irregular - android

Update: After some digging I managed to find some information in the Logcat. See bottom.
Edit 2:
I have now created a new activity from scratch to reduce the problem. It does still not work correctly. Here is the code:
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
private boolean isInitialized = false;
private MainActivity mainActivity;
int ctr = 0;
private String words[] = {"ord", "kula", "fotboll"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mainActivity = this;
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
System.out.println("---onStart");
}
#Override
public void onDone(String utteranceId) {
System.out.println("-----onDone");
}
#Override
public void onError(String utteranceId) {
System.out.println("-----onError");
}
#Override
public void onError(String utteranceId, int errorCode){
onError(utteranceId);
System.out.println("Error with code: " + errorCode);
}
});
isInitialized = true;
Locale locale = new Locale("swe");
textToSpeech.setLanguage(locale);
}
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isInitialized){
System.out.println(textToSpeech.getLanguage().getDisplayLanguage());
textToSpeech.speak(words[ctr], TextToSpeech.QUEUE_FLUSH, null, "SpeakTest");
ctr++;
ctr %= words.length;
} else {
Snackbar.make(view, "Speaker not ready", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
}
}
What is extremely surprising is that only the words "ord" and "fotboll" are spoken, but not "kula". If I change words to {"kula", "kula", "kula"} and try long enough, it suddenly starts to work. As I have understood the documentation one should use the tags here. I have tried se, swe, sv, all with same result. Further, the command System.out.println(textToSpeech.getLanguage().getDisplayLanguage()); gives svenska which is correct.
If I change to en it works all the time. Also I get System.out.println(textToSpeech.getLanguage().getDisplayLanguage());
= engelska again correct.
What on earth is going on?
EDIT:
I have added a UtteranceProgressListener, according to this, the method
onError(String id) is deprecated and should be replaced by onError(String id, int errorCode). However, I have extended my class with UtteranceProgressListener and it forces me to implement the old onError method. This is always called, so something is wrong but I do not know what.
This is because the other onError(String id, int code) is never called.
I have updated the code.
I have a function that is supposed to speak a word in a specific language when the function is called.
Until for a couple of days ago it worked fine in my Sony Compact XZ2, but now it is irregular. Sometimes the word is spoken and sometimes not. The command textToSpeech.getEngines() returns com.google.android.tts
For example for Swedish, in setLanguage, I have tried "sv" and "sv-SV" when I create the Locale object. That has not helped.
I just noticed that when I press the button that calls playWord(text) a bunch of times ( > 40) it works and sometimes it works directly. Seems to be some strange delay.
The function speakText is called from this function in my Fragment:
private void playWord(){
if (text2Speech.isReady()) {
text2Speech.checkSpeaking();
text2Speech.setLanguage(getAcronym(mTraining.getCurrentSrc()));
text2Speech.speakText(front);
} else {
Toast.makeText(getContext(),"Speaker not ready yet", Toast.LENGTH_SHORT).show();
}
}
This is the class that handles the speaking. I have not obtained any error messages. It just seems random when the speaker works.
public class Text2Speech extends UtteranceProgressListener {
private Context mContext;
private TextToSpeech textToSpeech;
private boolean isReady = false;
public Text2Speech(Context context, final String src){
mContext = context;
System.out.println("text2Speech created");
textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
isReady = true;
Locale locale = new Locale(src);
int ttsLang = textToSpeech.setLanguage(locale);
if (ttsLang == TextToSpeech.LANG_MISSING_DATA
|| ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "The Language is not supported!");
} else {
Log.i("TTS", "Language Supported.");
}
Log.i("TTS", "Initialization success.");
} else {
Toast.makeText(mContext, "TTS Initialization failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean isReady(){
return isReady;
}
public void checkSpeaking(){
if (textToSpeech.isSpeaking()){
textToSpeech.stop();
}
}
public void showMessage(String msg){
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
public void speakText(String text){
int speechStatus = textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
switch (speechStatus){
case TextToSpeech.ERROR_INVALID_REQUEST:
showMessage("Invalid Request");
break;
case TextToSpeech.ERROR_NETWORK:
showMessage("Network Error");
break;
case TextToSpeech.ERROR_NETWORK_TIMEOUT:
showMessage("Network Timeout");
break;
case TextToSpeech.ERROR_NOT_INSTALLED_YET:
showMessage("Error Not Yet Downloaded");
break;
case TextToSpeech.ERROR_OUTPUT:
showMessage("Output Error");
break;
case TextToSpeech.ERROR_SERVICE:
showMessage("Error of TTS service");
break;
case TextToSpeech.ERROR_SYNTHESIS:
showMessage("Error synthesizing");
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
showMessage("Language nor supported");
break;
}
if (speechStatus == TextToSpeech.ERROR) {
Log.e("TTS", "Error in converting Text to Speech!");
}
System.out.println("speech status - text " + speechStatus + " - " + text);
}
public void setLanguage(String src){
Locale locale = new Locale(src);
int tts = textToSpeech.setLanguage(locale);
System.out.println(tts + " " + src);
if (tts == TextToSpeech.LANG_MISSING_DATA
|| tts == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(mContext, "Language not yet supported.", Toast.LENGTH_LONG).show();
}
}
public void stop(){
textToSpeech.stop();
textToSpeech.shutdown();
}
#Override
public void onStart(String utteranceId) {
Log.e("START", "start speaking");
}
#Override
public void onDone(String utteranceId) {
Log.e("DONE", "done speaking");
}
#Override
public void onError(String utteranceID){
Log.e("Error", "Not infromative");
}
// This is not called!
#Override
public void onError(String utteranceId, int errorCode) {
Log.e("Error", "Error speaking");
}
}
Here is the error message in the Logcat:
NetworkSynthesizer: ExecutionException during NetworkFetchTask
java.util.concurrent.ExecutionException: clx: RESOURCE_EXHAUSTED: Quota exceeded for quota metric 's3-sessions' and limit 's3-session-limit' of service 'speechs3proto2-pa.googleapis.com' for consumer 'project_number:529030122437'.
at java.util.concurrent.FutureTask.report(FutureTask.java:123)
at java.util.concurrent.FutureTask.get(FutureTask.java:207)
at avf.a(PG:37)
at avf.a(PG:154)
at com.google.android.tts.service.GoogleTTSService.onSynthesizeText(PG:250)
at android.speech.tts.TextToSpeechService$SynthesisSpeechItem.playImpl(TextToSpeechService.java:1033)
at android.speech.tts.TextToSpeechService$SpeechItem.play(TextToSpeechService.java:819)
at android.speech.tts.TextToSpeechService$SynthHandler$1.run(TextToSpeechService.java:583)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:280)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: clx: RESOURCE_EXHAUSTED: Quota exceeded for quota metric 's3-sessions' and limit 's3-session-limit' of service 'speechs3proto2-pa.googleapis.com' for consumer 'project_number:529030122437'.
at cze.a(PG:58)
at cze.a(PG:29)
at dao.a(PG:21)
at ave.a(PG:36)
at ave.call(PG:80)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
2019-03-16 21:35:46.917 1356-5238/? E/ActivityManager: Sending non-protected broadcast com.sonymobile.intent.action.POWER_BACK_OFF_FACTOR_CHANGED from system 2179:com.android.phone/1001 pkg com.android.phone
java.lang.Throwable
at com.android.server.am.ActivityManagerService.checkBroadcastFromSystem(ActivityManagerService.java:21814)
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:22423)
at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:22565)
at android.app.IActivityManager$Stub.onTransact$broadcastIntent$(IActivityManager.java:10171)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:167)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3416)
at android.os.Binder.execTransact(Binder.java:731)
2019-03-16 21:35:46.917 12061-13318/? E/TTS.GoogleTTSServiceImp: Synthesis failure with error status code: -4
2019-03-16 21:35:46.918 12061-13318/? W/PlaybackSynthesisRequest: done() was called before start() call
2019-03-16 21:35:46.919 6468-6489/com.erikbylow.tailoreddictfire D/SPEECH: Error
When I turn WiFi on, it works.
Speculation: Can it be that the languages were missing and not downloaded when I did not use WiFi? When I turned WiFi on, the languages were downloaded?
To me this error: clx: RESOURCE_EXHAUSTED: Quota exceeded for quota metric... looks like there was always a network request, but after turning WiFi on, I could use TextToSpeach in flight mode.
On the other hand, I tried it woth russian in flight mode, that did not work. I turned internet on without WiFi, then it worked. Turned WiFi on again and then Russian worked as well. At least this suggests something needed to be downloaded?
I would like to find out what causes the problem and how to solve it since it is an app on Google Play. (Although I currently have exactly 0 active users beside me...) :).

Just to give some closure: This was an Android Framework Bug that has been closed with Android 12 (API 31). See also my referenced Bug Ticket: https://issuetracker.google.com/issues/138321382?pli=1

Related

When I use TTS, occur setLanguage failed on android

I use TTS (Text to speech) but my device not speak voice.
TtsEngine::setLanguage called with unsupported language
setLanguage(kor, KOR,) failed
I think my device not supported korean language.
how to install korean language pack?
my source.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create TTS
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i != ERROR) {
tts.setLanguage(Locale.KOREAN);
}
else {
}
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
}
How to speak on my android device ?
thanks:..
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS ) {
{
int result = tts.setLanguage(Locale.getDefault());
System.out.println ( "Result : " + result + " " + Locale.getDefault().getLanguage() );
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText( this , "Please Set your Language to English US.", Toast.LENGTH_LONG ).show();
}
else
{
tts.setLanguage(Locale.KOREAN);//remove this line
tts.setLanguage(new Locale("ko_KR"));//add this line
tts.speak( "hey",TextToSpeech.QUEUE_FLUSH, null );
}
}
});
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
Short answer: Try to turn the WIFI on
I had the same problem.
If you filter the word "TTS" in the logcat, you can see that the device is trying to download the locale. So, if you turn on Wifi, your problem should be solved. I'm looking for a solution to load the Locale locally on the device to avoid downloading it over the Internet.

Text-To-Speech Initialization Delay

I am trying to add text-to-speech feature to my app, and it is working fine until I updated TTS from Google Play store.
There wasn't any delay to initialize the TTS in onCreate Method.
After the update, it would take 3-5 seconds for this TTS to finish initializing.
Basically, the text-to-speech is not ready until 3-5 seconds later.
Can someone please tell me what I've done wrong?
private HashMap<String, String> TTS_ID = new HashMap<String, String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
.....
TextToSpeech_Initialize();
}
public void TextToSpeech_Initialize() {
TTS_ID.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
speech = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
speech.setSpeechRate(SpeechRateValue);
speech.speak(IntroSpeech, TextToSpeech.QUEUE_FLUSH, TTS_ID);
}
}
});
}
Thank you very much
Confirmed! This is an issue with Google text to speech engine, if you try any other tts the delay disappears, eg Pico tts.
I have stumbled across this problem before but now I have found a proper solution..
You can initialize TextToSpeach in onCreate() like this:
TextToSpeach textToSpeech = new TextToSpeech(this, this);
but first you need to implement TextToSpeech.OnInitListener, and then you need to override the onInit() method:
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();
} else {
button.setEnabled(true);
}
} else {
Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();
}
}
I also noticed that if you didn't set the language in onInit() there is gonna be a delay!!
And now you can write the method that says the text:
private void speakOut(final String detectedText){
if(textToSpeech !=null){
textToSpeech.stop(); //stop and say the new word
textToSpeech.speak(detectedText ,TextToSpeech.QUEUE_FLUSH, null, null);
}
}

SpeechRecognizer throws onError on the first listening

In the Android 5 I faced with strange problem. The first call to the startListening of SpeechRecognizer results to the onError with error code 7 (ERROR_NO_MATCH).
I made test app with the following code:
if (speechRecognizer == null) {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle bundle) {
Log.d(TAG, "onReadyForSpeech");
}
#Override
public void onBeginningOfSpeech() {
Log.d(TAG, "onBeginningOfSpeech");
}
#Override
public void onRmsChanged(float v) {
Log.d(TAG, "onRmsChanged");
}
#Override
public void onBufferReceived(byte[] bytes) {
Log.d(TAG, "onBufferReceived");
}
#Override
public void onEndOfSpeech() {
Log.d(TAG, "onEndOfSpeech");
}
#Override
public void onError(int i) {
Log.d(TAG, "onError " + i);
}
#Override
public void onResults(Bundle bundle) {
Log.d(TAG, "onResults");
}
#Override
public void onPartialResults(Bundle bundle) {
Log.d(TAG, "onPartialResults");
}
#Override
public void onEvent(int i, Bundle bundle) {
Log.d(TAG, "onEvent");
}
});
}
final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
speechRecognizer.startListening(sttIntent);
And have this log messages after first startListening call:
onError 7
onReadyForSpeech
onBeginningOfSpeech
onEndOfSpeech
onResults
And following messages after another startListening calls:
onRmsChanged
...
onRmsChanged
onReadyForSpeech
onRmsChanged
...
onRmsChanged
onBeginningOfSpeech
onRmsChanged
...
onRmsChanged
onEndOfSpeech
onRmsChanged
onRmsChanged
onRmsChanged
onResults
So, what is the reason of this error and how do I fix it?
As soon as you configure the "Okay Google" function to every screen the error appears.
So this seems to be the reason!
Deactivate the function and the problem should be solved
Done one workaround.
This is a regular flow
onReadyForSpeech -->onBeginningOfSpeech-->onEndOfSpeech -->onResults
But weired flow
onError(no match) -->onReadyForSpeech -->onBeginningOfSpeech-->onEndOfSpeech -->onResults
So set a boolean on the end of speech to true. and check onError to make sure that it has thrown an error after an end of speech!
speech.startListening(recognizerIntent);
isEndOfSpeech = false;
#Override
public void onError(int error) {
if (!isEndOfSpeech)
return;
}
#Override
public void onEndOfSpeech() {
isEndOfSpeech = true;
}
I had the same problem but I couldn't find a workaround, so I ended up just calling return inside onError if the time between startListening and onError is unreasonably short.
protected long mSpeechRecognizerStartListeningTime = 0;
protected synchronized void speechRecognizerStartListening(Intent intent) {
if (mSpeechRecognizer != null) {
this.mSpeechRecognizerStartListeningTime = System.currentTimeMillis();
RLog.d(this, "speechRecognizerStartListening");
this.mSpeechRecognizer.startListening(intent);
}
}
...
#Override
public synchronized void onError(int error) {
RLog.i(this, this.hashCode() + " - onError:" + error);
// Sometime onError will get called after onResults so we keep a boolean to ignore error also
if (mSuccess) {
RLog.w(this, "Already success, ignoring error");
return;
}
long duration = System.currentTimeMillis() - mSpeechRecognizerStartListeningTime;
if (duration < 500 && error == SpeechRecognizer.ERROR_NO_MATCH) {
RLog.w(this, "Doesn't seem like the system tried to listen at all. duration = " + duration + "ms. This might be a bug with onError and startListening methods of SpeechRecognizer");
RLog.w(this, "Going to ignore the error");
return;
}
// -- actual error handing code goes here.
}
I had the same problem on several devices. It seems onError(7) is always called before onReadyForSpeech(), so if to avoid using ugly times, you can do something like:
public void start(){
performingSpeechSetup = true;
speechRecognizer.startListening(intent);
}
and in the RecognitionListener:
public void onReadyForSpeech(Bundle bundle) {
performingSpeechSetup = false;
}
#Override
public void onError(int error) {
if (performingSpeechSetup && error == SpeechRecognizer.ERROR_NO_MATCH) return;
// else handle error
}
Turned out to be very easy in my case. The launching sound of the voice recognition was too loud and triggered the listening process at the very beginning. Turn down the system sound would help. (The volume key)

Recognizing multiple keywords using PocketSphinx

I've installed the PocketSphinx demo and it works fine under Ubuntu and Eclipse, but despite trying I can't work out how I would add recognition of multiple words.
All I want is for the code to recognize single words, which I can then switch() within the code, e.g. "up", "down", "left", "right". I don't want to recognize sentences, just single words.
Any help on this would be grateful. I have spotted other users' having similar problems but nobody knows the answer so far.
One thing which is baffling me is why do we need to use the "wakeup" constant at all?
private static final String KWS_SEARCH = "wakeup";
private static final String KEYPHRASE = "oh mighty computer";
.
.
.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
What has wakeup got to do with anything?
I have made some progress (?) : Using addGrammarSearch I am able to use a .gram file to list my words, e.g. up,down,left,right,forwards,backwards, which seems to work well if all I say are those particular words. However, any other words will cause the system to match what is said to the "nearest" word from those stated. Ideally I don't want recognition to occur if words spoken are not in the .gram file...
Thanks to Nikolay's tip (see his answer above), I have developed the following code which works fine, and does not recognize words unless they're on the list. You can copy and paste this directly over the main class in the PocketSphinxDemo code:
public class PocketSphinxActivity extends Activity implements RecognitionListener
{
private static final String DIGITS_SEARCH = "digits";
private SpeechRecognizer recognizer;
#Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setContentView(R.layout.main);
((TextView) findViewById(R.id.caption_text)).setText("Preparing the recognizer");
try
{
Assets assets = new Assets(PocketSphinxActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
}
catch (IOException e)
{
// oops
}
((TextView) findViewById(R.id.caption_text)).setText("Say up, down, left, right, forwards, backwards");
reset();
}
#Override
public void onPartialResult(Hypothesis hypothesis)
{
}
#Override
public void onResult(Hypothesis hypothesis)
{
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null)
{
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBeginningOfSpeech()
{
}
#Override
public void onEndOfSpeech()
{
reset();
}
private void setupRecognizer(File assetsDir)
{
File modelsDir = new File(assetsDir, "models");
recognizer = defaultSetup().setAcousticModel(new File(modelsDir, "hmm/en-us-semi"))
.setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
.getRecognizer();
recognizer.addListener(this);
File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
}
private void reset()
{
recognizer.stop();
recognizer.startListening(DIGITS_SEARCH);
}
}
Your digits.gram file should be something like:
up /1e-1/
down /1e-1/
left /1e-1/
right /1e-1/
forwards /1e-1/
backwards /1e-1/
You should experiment with the thresholds within the double slashes // for performance, where 1e-1 represents 0.1 (I think). I think the maximum is 1.0.
And it's 5.30pm so I can stop working now. Result.
you can use addKeywordSearch which uses to file with keyphrases. One phrase per line with threshold for each phrase in //, for example
up /1.0/
down /1.0/
left /1.0/
right /1.0/
forwards /1e-1/
Threshold must be selected to avoid false alarms.
Working on updating Antinous amendment to the PocketSphinx demo to allow it to run on Android Studio. This is what I have so far,
//Note: change MainActivity to PocketSphinxActivity for demo use...
public class MainActivity extends Activity implements RecognitionListener {
private static final String DIGITS_SEARCH = "digits";
private SpeechRecognizer recognizer;
/* Used to handle permission request */
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
((TextView) findViewById(R.id.caption_text))
.setText("Preparing the recognizer");
// Check if user has given permission to record audio
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
return;
}
new AsyncTask<Void, Void, Exception>() {
#Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(MainActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
#Override
protected void onPostExecute(Exception result) {
if (result != null) {
((TextView) findViewById(R.id.caption_text))
.setText("Failed to init recognizer " + result);
} else {
reset();
}
}
}.execute();
((TextView) findViewById(R.id.caption_text)).setText("Say one, two, three, four, five, six...");
}
/**
* In partial result we get quick updates about current hypothesis. In
* keyword spotting mode we can react here, in other modes we need to wait
* for final result in onResult.
*/
#Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null) {
return;
} else if (hypothesis != null) {
if (recognizer != null) {
//recognizer.rapidSphinxPartialResult(hypothesis.getHypstr());
String text = hypothesis.getHypstr();
if (text.equals(DIGITS_SEARCH)) {
recognizer.cancel();
performAction();
recognizer.startListening(DIGITS_SEARCH);
}else{
//Toast.makeText(getApplicationContext(),"Partial result = " +text,Toast.LENGTH_SHORT).show();
}
}
}
}
#Override
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), "Hypothesis" +text, Toast.LENGTH_SHORT).show();
}else if(hypothesis == null){
makeText(getApplicationContext(), "hypothesis = null", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onDestroy() {
super.onDestroy();
recognizer.cancel();
recognizer.shutdown();
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onEndOfSpeech() {
reset();
}
#Override
public void onTimeout() {
}
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
recognizer = defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
// .setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
.getRecognizer();
recognizer.addListener(this);
File digitsGrammar = new File(assetsDir, "digits.gram");
recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);
}
private void reset(){
recognizer.stop();
recognizer.startListening(DIGITS_SEARCH);
}
#Override
public void onError(Exception error) {
((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
}
public void performAction() {
// do here whatever you want
makeText(getApplicationContext(), "performAction done... ", Toast.LENGTH_SHORT).show();
}
}
Caveat emptor: this is a work in progress. Check back later. Suggestions would be appreciated.

Implement barge-in for Android TTS

I am having difficulty figuring out how to resolve this issue, I am not sure if I am not setting up threads correctly or if it is even possible to resolve things properly.
This is an Android app that reads certain strings out as TTS (using the native Android TTS) at certain timings. During this TTS reading, the user should be able to barge-in with instructions such as "Stop" or "Pause." This recognition is done by using the iSpeech API.
Our current solution is to have the TTS running as a Thread that will output the proper strings. Once the user presses a button to begin the voice recognition (using an Intent), the app does voice recognition and handles it perfectly, but then TTS never again outputs anything. Logcat shows the following error:
11-28 02:18:57.072: W/TextToSpeech(16383): speak failed: not bound to TTS engine
I have thought about making the voice recognition a thread of its own that pauses the TTS, but the problem would then be that the timer controlling the TTS would become unsynced with what it should be.
Any advice or help would be appreciated.
Relevant code regarding the thread and the intent are below:
Thread
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Prevent device from sleeping mid build.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_build_order);
mPlayer = MediaPlayer.create(BuildOrderActivity.this, R.raw.bing);
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
tts = new TextToSpeech(BuildOrderActivity.this, new TextToSpeech.OnInitListener() {
#SuppressWarnings("deprecation")
public void onInit(int status) {
if(status != TextToSpeech.ERROR)
{
tts.setLanguage(Locale.US);
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
public void onUtteranceCompleted(String utteranceId) {
mPlayer.start();
}
});
}
}
});
buttonStart = (Button) findViewById(R.id.buttonStartBuild);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startBuild = new StartBuildRunnable();
Thread t = new Thread(startBuild);
t.start();
}
});
...//code continues oncreate setup for the view}
public class StartBuildRunnable implements Runnable {
public void run() {
double delay;
buildActions = parseBuildXMLAction();
buildTimes = parseBuildXMLTime();
say("Build has started");
delayForNextAction((getSeconds(buildTimes.get(0)) * 1000));
say(buildActions.get(0));
for (int i = 1; i < buildActions.size(); i++)
{
delay = calcDelayUntilNextAction(buildTimes.get(i - 1), buildTimes.get(i));
delayForNextAction((long) (delay * 1000));
say(buildActions.get(i));
//listViewBuildItems.setSelection(i);
}
say("Build has completed");
}
}
Intent
/**
* Fire an intent to start the speech recognition activity.
* #throws InvalidApiKeyException
*/
private void startRecognition() {
setupFreeFormDictation();
try {
recognizer.startRecord(new SpeechRecognizerEvent() {
#Override
public void onRecordingComplete() {
updateInfoMessage("Recording completed.");
}
#Override
public void onRecognitionComplete(SpeechResult result) {
Log.v(TAG, "Recognition complete");
//TODO: Once something is recognized, tie it to an action and continue recognizing.
// currently recognizes something in the grammar and then stops listening until
// the next button press.
if (result != null) {
Log.d(TAG, "Text Result:" + result.getText());
Log.d(TAG, "Text Conf:" + result.getConfidence());
updateInfoMessage("Result: " + result.getText() + "\n\nconfidence: " + result.getConfidence());
} else
Log.d(TAG, "Result is null...");
}
#Override
public void onRecordingCancelled() {
updateInfoMessage("Recording cancelled.");
}
#Override
public void onError(Exception exception) {
updateInfoMessage("ERROR: " + exception.getMessage());
exception.printStackTrace();
}
});
} catch (BusyException e) {
e.printStackTrace();
} catch (NoNetworkException e) {
e.printStackTrace();
}
}

Categories

Resources