最新消息:20210917 已从crifan.com换到crifan.org

【已解决】rcsjta项目编译出错:TextToSpeech中的speak(String,int,HashMap)已过时

编译 crifan 605浏览 0评论
折腾:
【未解决】Android Studio中gradle编译rcjsta项目多个使用或覆盖了已过时的API等报错
期间,继续解决其他的问题
rcsjta/samples/api/tts/src/com/orangelabs/rcs/tts/PlayTextToSpeech.java:98: 警告: [deprecation] TextToSpeech中的speak(String,int,HashMap<String,String>)已过时
                tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                   ^
去看定义
果然是:Deprecated
推荐是:
* @deprecated As of API level 21, replaced by
*         {@link #speak(CharSequence, int, Bundle, String)}.
*/
但是如何改,需要好好研究一下逻辑
texttospeech speak deprecated
在当前文件中找到实现了
android/speech/tts/TextToSpeech.java
/**
 * Speaks the text using the specified queuing strategy and speech parameters, the text may
 * be spanned with TtsSpans.
 * This method is asynchronous, i.e. the method just adds the request to the queue of TTS
 * requests and then returns. The synthesis might not have finished (or even started!) at the
 * time when this method returns. In order to reliably detect errors during synthesis,
 * we recommend setting an utterance progress listener (see
 * {@link #setOnUtteranceProgressListener}) and using the
 * {@link Engine#KEY_PARAM_UTTERANCE_ID} parameter.
 *
 * @param text The string of text to be spoken. No longer than
 *            {@link #getMaxSpeechInputLength()} characters.
 * @param queueMode The queuing strategy to use, {@link #QUEUE_ADD} or {@link #QUEUE_FLUSH}.
 * @param params Parameters for the request. Can be null.
 *            Supported parameter names:
 *            {@link Engine#KEY_PARAM_STREAM},
 *            {@link Engine#KEY_PARAM_VOLUME},
 *            {@link Engine#KEY_PARAM_PAN}.
 *            Engine specific parameters may be passed in but the parameter keys
 *            must be prefixed by the name of the engine they are intended for. For example
 *            the keys "com.svox.pico_foo" and "com.svox.pico:bar" will be passed to the
 *            engine named "com.svox.pico" if it is being used.
 * @param utteranceId An unique identifier for this request.
 *
 * @return {@link #ERROR} or {@link #SUCCESS} of <b>queuing</b> the speak operation.
 */
public int speak(final CharSequence text,
                 final int queueMode,
                 final Bundle params,
                 final String utteranceId) {
    return runAction(new Action<Integer>() {
        @Override
        public Integer run(ITextToSpeechService service) throws RemoteException {
            Uri utteranceUri = mUtterances.get(text);
            if (utteranceUri != null) {
                return service.playAudio(getCallerIdentity(), utteranceUri, queueMode,
                        getParams(params), utteranceId);
            } else {
                return service.speak(getCallerIdentity(), text, queueMode, getParams(params),
                        utteranceId);
            }
        }
    }, ERROR, "speak");
}
TextToSpeech  |  Android 开发者  |  Android Developers
新的:
speak(CharSequence text, int queueMode, Bundle params, String utteranceId)
Speaks the text using the specified queuing strategy and speech parameters, the text may be spanned with TtsSpans.
旧的:
int
speak(String text, int queueMode, HashMap<String, String> params)
This method was deprecated in API level 21. As of API level 21, replaced byspeak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String).
java – Android TTS sound Leaked Service Connection and speak deprecated – Stack Overflow
android – TextToSpeech : deprecated speak function in API Level 21 – Stack Overflow
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);
}
好像直接用null即可?
把:
tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
改为:
tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
试试:
结果红色错误:
去看提示,选第二个:
再把之前的加上:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
} else {
    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
}
然后重新编译试试
竟然还是会报错:
看来,虽然此处代码逻辑没问题了
但是要彻底杜绝警告,估计只能参考
android – TextToSpeech with API 21 – Stack Overflow
去弄成独立函数,然加上@SuppressWarnings(“deprecation”)了。
@SuppressWarnings("deprecation")
private void ttsUnder20(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null);
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null, null);
}
调用:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
                    ttsUnder20(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                } else {
//                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                    ttsGreater21(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                }
但是:
【未解决】Android项目中@TargetApi语法报错:Cannot resolve symbol TargetApi
不过,突然想到,既然Android Studio足够智能,是否是,给上面依旧报错的代码,直接加上sdk版本的if判断,是否就可以消除警告了?
去试试能否实现具体写法
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
//                    ttsUnder20(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                } else {
                    @SuppressWarnings("deprecation")
                    {
                        tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                    }
//                    ttsGreater21(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                }
结果提示:
Annotations are not allowed here
不支持这么写
对单独一行代码这么写
另外也试了试:
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//                    @SuppressWarnings("deprecation")
//                    {
                        tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                    }
问题依旧
所以放弃这条路。
现在已解决:
【已解决】Android项目中@TargetApi语法报错:Cannot resolve symbol TargetApi
然后重新编译,问题解决了。
【总结】
此处之前代码:
tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
报错:
[deprecation] TextToSpeech中的speak(String,int,HashMap<String,String>)已过时
                tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                   ^
原因:
TextToSpeech中的speak
已废弃旧的:
int speak(final String text, final int queueMode, final HashMap<String, String> params)
推荐用新的:
int speak(final CharSequence text,
                 final int queueMode,
                 final Bundle params,
                 final String utteranceId) 
解决办法:
直接改为新的函数写法即可。
比如:
tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
但是有个问题:
就不支持旧的api
所以为了兼容,写成:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
                } else {
                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
                }
但是有个问题:
Android Studio编译后,对于:
tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
还是警告错误
所以为了规避警告的错误
写成:
import android.annotation.TargetApi;

@SuppressWarnings("deprecation")
private void ttsUnder20(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null);
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null, null);
}

public void onInit(int status) {
。。。
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ttsUnder20(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                } else {
                    ttsGreater21(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                }
才最终规避了警告。
【后记】
不过,有个奇怪的事情:
之前的的另一处:
if (messages.size() > 1) {
    for (int i = 1; i < messages.size(); i++) {
        tts.speak(messages.get(i), TextToSpeech.QUEUE_ADD, null);
    }
}
中的speak:
        tts.speak(messages.get(i), TextToSpeech.QUEUE_ADD, null);
也是和前面一样会报错的。
但是上述代码改动后,android studio重新编译后,竟然下面这个speak不报错了,很是奇怪。
不过后来再次重新编译,又报错了:
是对的,也是我们希望的。

(不过)为了代码完整性和一致性,也去改掉:
不过同时也发现前面代码写错了,20和21的调用写错了。
最终改为:
import android.annotation.TargetApi;
。。。

public class PlayTextToSpeech extends Service implements OnInitListener {
。。。

@SuppressWarnings("deprecation")
private void ttsUnder20(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null);
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String msgText, final int queueMode) {
    tts.speak(msgText, queueMode, null, null);
}


    /**
     * TTS engine init
     * 
     * @param status Status
     */
    public void onInit(int status) {
        if ((tts != null) && (status == TextToSpeech.SUCCESS)) {
            Log.v(TAG, "TTS engine initialized with success");
            if ((messages != null) && (messages.size() > 0)) {
                // Speak
                Log.v(TAG, "Start TTS session: play " + messages.size() + " messages");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null, null);
                    ttsGreater21(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                } else {
//                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//                    @SuppressWarnings("deprecation")
//                    {
//                        tts.speak(messages.get(0), TextToSpeech.QUEUE_FLUSH, null);
//                    }
                    ttsUnder20(messages.get(0), TextToSpeech.QUEUE_FLUSH);
                }
                if (messages.size() > 1) {
                    for (int i = 1; i < messages.size(); i++) {
//                        tts.speak(messages.get(i), TextToSpeech.QUEUE_ADD, null);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            ttsGreater21(messages.get(i), TextToSpeech.QUEUE_ADD);
                        } else {
                            ttsUnder20(messages.get(i), TextToSpeech.QUEUE_ADD);
                        }
                    }
                }

。。。
            }
        }
    }
即可。

转载请注明:在路上 » 【已解决】rcsjta项目编译出错:TextToSpeech中的speak(String,int,HashMap)已过时

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.187 seconds, using 23.22MB memory