【问题标题】:TextToSpeech : deprecated speak function in API Level 21TextToSpeech : API Level 21 中已弃用的说话功能
【发布时间】:2015-08-22 18:43:06
【问题描述】:

我尝试在我的应用中使用 TextToSpeech,

String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

但是函数 speak(String text, int queueMode, HashMap params) 在 API 级别 21 中已被弃用。相反,建议使用 speak(CharSequence text, int queueMode, Bundle params, String utteranceId)。 但我不知道如何设置它。谢谢

【问题讨论】:

    标签: android text-to-speech


    【解决方案1】:
    String text = editText.getText().toString();
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
    } else {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    

    【讨论】:

    • 当我使用您的建议时,Android Studio 会说:“params="null" utteranceId="null" 并打开 if 语句建议。而且,它对我的​​情况没有足够的帮助。
    • Build.VERSION_CODES 现已过时(2021 年 4 月)。只需替换上述条件 (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) ...等
    【解决方案2】:

    这是适合我的完整作品

    Private TextToSpeech ts
     ts=new TextToSpeech(CurrentActivity.this, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    String text = "Any Text to Speak";
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        ts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
                    } else {
                        ts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                    }
    
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2018-06-04
      相关资源
      最近更新 更多