1

I am trying to find out why my app does not work as expected. To find out why, I have created a small project with a single Activity where if you press a button, predefined words are supposed to be spoken. I am using a Sony Xperia Compact Android 9. I made some system update lately, can that be a reason?

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private TextToSpeech textToSpeech;
    private boolean isInitialized = false;
    private MainActivity mainActivity;
    int ctr = 0;
    private String words[] = {"handboll", "kula", "golf"};
    @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("sv");
                    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 happens is that only a few of the words are spoken when I press the button. For example, in this case only handboll and kula could be said but not golf. In other cases kula could not be said.

Then I created the array {"kula", "kula", "kula"} and pressed the button maybe 100 times, then suddenly it started to work. So obviously it can speak the words, but it does not always happen when I press the button.

For the Locale object, I have tried sv, sv-SV and swe, all with the same result.

If I use english and en, it works fine.

El_Loco
  • 1,716
  • 4
  • 20
  • 35
  • Did you try another device? – Hamid Goodarzi Mar 14 '19 at 22:34
  • Yes, seems to work on a Huawei with Android 7 – El_Loco Mar 15 '19 at 20:46
  • I tested this and it seems to work properly... the behavior you experienced probably has to do with the varying behavior of speech engines and even varying behavior of voices. Some voices are "online" and require at east an initial download when speak() is called... causing a significant delay. – Nerdy Bunz Jan 08 '20 at 03:59

1 Answers1

0

I had the same problem with Thai in Ankidroid. Usually no speech but if I use gestures to repeat the speech often enough it sometimes worrks. English works fine. I uninstalled the updates for Google text to speech and it works again.

Larry
  • 1