文本到语音android在可见活动时输出语音

问题描述 投票:0回答:2

我正在为视障人士构建一个android本机应用程序,并且我想使用android TTS-android.speech.tts.TextToSpeech来指导用户使用我的应用程序。单击按钮后,我成功显示了语音,但是我也想在活动可见后输出欢迎消息。这是一个代码片段:


public class MainActivity extends AppCompatActivity  {
    private TextToSpeech textToSpeech;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status==TextToSpeech.SUCCESS){
                    int result=textToSpeech.setLanguage(Locale.getDefault());
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    } else {
                        Log.d("TTS","Speech initialized");
                    }
                } else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });
        speak("welcome");
        speak("Click on the button to begin settings ");

        audio.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                speak("this is a test");
            }
        });
    }
    public void speak(final String S){
    textToSpeech.speak(S,TextToSpeech.QUEUE_ADD,null);
    }
}
java android text-to-speech
2个回答
0
投票

问题是tts尚未初始化。

最快的解决方法是在inInit中移动介绍性语音命令:

@Override
            public void onInit(int status) {
                if (status==TextToSpeech.SUCCESS){
                    int result=textToSpeech.setLanguage(Locale.getDefault());
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    } else {
                        Log.d("TTS","Speech initialized");

                        speak("welcome");
                        speak("Click on the button to begin settings ");
                    }
                } else {
                    Log.e("TTS", "Initialization failed");
                }
            }

0
投票

基于[https://stackoverflow.com/a/48354182/13518237][1],我设法使其正常工作。这是代码

public class Speech  implements TextToSpeech.OnInitListener {
    private boolean initialized;
    private String queuedText;
    private String TAG = "TTS";
    private TextToSpeech tts; //text to speech

    public Speech() {
    }

    public TextToSpeech getTts() {
        return tts;
    }

    public void setTts(TextToSpeech tts) {
        this.tts = tts;
    }
    public void speak(String text) {
        if (!initialized) {
            queuedText = text;
            return;
        }
        queuedText = null;
        HashMap <String, String> params= new HashMap();
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"Done Speaking");
        tts.speak(text, TextToSpeech.QUEUE_ADD, params);
    }
    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            initialized = true;
            tts.setLanguage(Locale.ENGLISH);
            if (queuedText != null) {
                speak(queuedText);
            }
        }
    }
}
public class Menu extends AppCompatActivity {
    private RelativeLayout audio;
    private Speech speech = new Speech();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        audio= findViewById(R.id.audio);
        speech.setTts(new TextToSpeech(this, speech));
        speech.speak("This is the menu");
 audio.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                speak("this is a test");
            }
        });
    }

    @Override
    public void onDestroy() {
        if (speech.getTts()!=null){
            speech.getTts().stop();
            speech.getTts().shutdown();
        }
        super.onDestroy();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.