TextToSpeech initialization on Android — what if it fails?

£可爱£侵袭症+ 提交于 2021-01-28 05:54:44

问题


In Android, if a TextToSpeech instance fails to initialize (the callback invoked indicating the completion of the TextToSpeech engine initialization returns TextToSpeech.ERROR), does that mean that subsequent attempted method calls to that instance will result in null pointer exceptions?

Example...

We initialize the object:

t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.ERROR) {
                    Log.i("XXX", "There was an error initializing the TTS");
                }
            }
        });

... let's say the init fails, but we then go on to do something like:

t1.speak("hello");

Will this crash... or just say nothing? The reason I ask is that if it does crash, then obviously I would have to put null checks everywhere.

Also, the reason I can't just find out for myself is that I don't know how to simulate the error.

Thanks for any prior knowledge.


回答1:


For your main question read the docs of the speak() method (here):

This method is asynchronous, i.e. the method just adds the request to the queue of TTS requests and then returns.

So unless your tts instance is null, it shouldn't throw any exception or crash the app, but just return the error code.

Also, the reason I can't just find out for myself is that I don't know how to simulate the error.

Try to use constructor (see docs) that expects as third parameter String engine and put invalid package name there. It should probably result in error then. (or disable/uninstall all TTS engines on your device/emulator)

Important thing to note regarding constructor is:

In a case of a failure the listener may be called immediately, before TextToSpeech instance is fully constructed.

So unless the status is SUCCESS, you shouldn't touch your tts in the listener (of course you can use the tts afterwards as in your example) because it might not even be assigned / initialized yet.




回答2:


I had this problem, then I noticed that on some devices TTS may be deactivated. So I've just done the following

try {
        Intent intent = new Intent();
        intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(intent, 0);
    } catch(ActivityNotFoundException exception) {
        Uri uri = Uri.parse("market://details?id=" + "com.google.android.tts&hl=fr");
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        try {
            startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + "com.google.android.tts&hl=fr")));
        }
    } 



回答3:


You can try with this code.

Use language_codes from https://www.w3schools.com/tags/ref_language_codes.asp.

mLanguage = new Locale(language_codes);

tts = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                int result = tts.setLanguage(mLanguage);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("Text2SpeechWidget", result + " is not supported");
                }
            }
        }
    });


来源:https://stackoverflow.com/questions/50285916/texttospeech-initialization-on-android-what-if-it-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!