我使用一个简单的代码来使用文本到语音:
package ch.yourclick.kitt.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.Locale;
import ch.yourclick.kitt.R;
/**
* A simple {@link Fragment} subclass.
* Use the {@link GeneralFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class GeneralFragment extends Fragment {
private TextToSpeech tts;
public GeneralFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment General.
*/
// TODO: Rename and change types and number of parameters
public static GeneralFragment newInstance() {
GeneralFragment fragment = new GeneralFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
Button hello = view.findViewById(R.id.hello);
// Text to speech
tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) { // <-- I never get into that if statement
int result = tts.setLanguage(Locale.getDefault());
// Language is not supported
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
}
}
else {
Log.e("TTS", "" + status); // Returns -1
Log.e("TTS", "" + TextToSpeech.SUCCESS); // Returns 0
}
}
});
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
speak();
}
});
return view;
}
/**
* Speak
*/
private void speak() {
String text = "Hello";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
/**
* Turn off
*/
@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}我收到这样的信息:
W/TextToSpeech:说话失败:不受TTS引擎的约束
我的问题是不能初始化文本到语音:
status返回-1,TextToSpeech.SUCCESS返回。
我使用的是Android,我的虚拟设备是Pixel2API 30。所以Google文本到语音引擎似乎是安装在上面的:

设置->辅助功能->文本到语音输出
如果我点击播放,我听到一个声音,所以我知道这不应该是一个设备问题。但是为什么它不能处理我的申请呢?我没有发现任何错误。
我不知道我做错了什么。如果你知道答案,或者对它有什么建议,请告诉我!
发布于 2020-10-08 02:58:07
针对使用文本到语音的Android 11的应用程序应该在清单中的查询元素中声明他说:
因此,将其添加到您的AndroidManifest.xml中:
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>在<application标记对我起作用之前添加这个。(Android说是Element queries is not allowed here )。
发布于 2020-07-30 21:01:49
正如日志中所说的“不绑定到TTS引擎”。在我这一边,它只是停用,这就是它不能绑定到TTS引擎的原因。在另一个帖子中看到我的答案,那里
发布于 2020-08-02 15:59:15
speak failed: not bound to TTS engine这可能不是error,只是打印一个普通的log。
但如果出现此日志,则可能无法执行语音通知。
在搜索信息之后,我发现解决这一问题的方法是:在实现onInit接口的OnInitListener方法中使用改写语音播放功能:
@Override
public void onInit(int status) {
textToSpeech.setSpeechRate(speechRate);
textToSpeech.setPitch(pitch);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}这样可以避免speak failed: not bound to TTS engine的错误。
https://stackoverflow.com/questions/63092645
复制相似问题