【问题标题】:How to apply the built in settings to the whole application如何将内置设置应用于整个应用程序
【发布时间】:2018-01-30 12:19:43
【问题描述】:

我正在制作一个具有文本到语音的 android 应用程序,并且我希望能够自定义它的语速。我已经有一个代码,但我不知道如何将语速应用于整个应用程序。

这是我为应用程序创建的设置。提前致谢! :D

public float getSpeechRate(){
    int checkedRadioButton = this.radioRate.getCheckedRadioButtonId();
    if (checkedRadioButton == R.id.rate_slow){
         return 0.5f;
    } else if (checkedRadioButton == R.id.rate_normal){
         return 1.0f;
    } else if(checkedRadioButton == R.id.rate_fast){
        return 1.5f;
    }
    return 0;
}

public void setSpeechRate(){
    float speechRate = this.getSpeechRate();
    if(speechRate == 0.5f){
        speakOut("This is a slow speech rate");
    } else if(speechRate == 1.0f){
        speakOut("This is a normal speech rate");
    } else {
        speakOut("This is a fast speech rate");
    }

这就是我将文本调用为语音的方式

toSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            Log.e("TTS", "TextToSpeech.OnInit...");
        }
    });

【问题讨论】:

  • 如何调用 tts 功能?
  • 上述语速方法仅适用于一项活动
  • 我在上面编辑了我如何创建文本到语音的代码
  • toSpeech.setSpeechRate(speechRate);
  • 您可以将值存储在您的SharedPreferences 中,请参阅此处stackoverflow.com/a/26928604/1256219

标签: android text-to-speech mobile-development


【解决方案1】:

做:

  • 创建一个“设置”页面供用户选择费率。
  • 按照@brandall 的建议将所选值存储在 SharedPreferences 中
  • 然后在您想使用 TTS 的任何 Activity 中,执行:

    TextToSpeech tts = TextToSpeechHelper.getTextToSpeech(CurrentActivity.this, customListener);

编辑 2

TextToSpeechHelper

 public class TextToSpeechHelper {

    private TextToSpeechHelper() {
        // Prevent the class instantiation
    }

    public static TextToSpeech getTextToSpeech(Context context, @NonNull CustomInitListener listener) {

        final TextToSpeech tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                Log.e("TTS", "TextToSpeech.OnInit...");

                if (status == TextToSpeech.SUCCESS) {
                    float rate = getSpeechRate(context);   
                    tts.setSpeechRate(rate);
                    listener.onSuccess();
                } else {
                    listener.onError();
                }
            }
        });


        return tts;
    }

    private static float getSpeechRate(Context context) {
        // Get the value stored in the shared preferences
        // ...
        return storedValue;
    }

    /**
     * Add a custom listener to perform actions when the TextToSpeech is initialized
     */
    public interface CustomInitListener {
        void onSuccess();

        void onError();
    }
}

【讨论】:

  • 如果每个Activity执行的动作不同,你可以将监听器传递给getTextToSpeech
  • 这是行不通的,因为在onInit 返回之前不会初始化 TTS 对象。在此之前,您无法将语言或其他参数(例如语速)应用于引擎。此外,如果方法是静态的,它应该返回一个静态 TTS 实例。
  • @brandall 你在第一部分是对的,但为什么实例应该是静态的?
  • 如果您在整个应用程序中使用静态帮助程序类,则 TTS 实例也应该是静态的 - 否则每次都会创建并返回一个新实例。如果它这样做了,那么暴露一个静态助手有什么意义呢?对 getTextToSpeech() 的每次调用都应返回单个静态全局托管 TTS 对象。只有当它为空时,才应该创建一个新的。
  • 不一定,每次需要的时候都可以实例化一个新的TTS。两种实现都是有效的。您只需要不要忘记在您不再需要的 TTS 上致电shutdown()。我不是单身人士的忠实粉丝(因为这显然是你的建议)
猜你喜欢
  • 2019-12-16
  • 2019-01-07
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
相关资源
最近更新 更多