gpt4 book ai didi

java - TTS : Speak fails

转载 作者:行者123 更新时间:2023-12-03 11:19:44 28 4
gpt4 key购买 nike

我正在使用一个简单的代码来使用文本转语音:

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: speak failed: not bound to TTS engine


我的问题是无法初始化文本到语音: status返回 -1 和 TextToSpeech.SUCCESS返回 0。
我正在使用 Android Studio,我的虚拟设备是 Pixel 2 API 30。所以 Google Text-to-speech Engine 似乎安装在它上面:
enter image description here
设置 -> 辅助功能 -> 文字转语音输出
如果我点击播放,我会听到一个声音,所以我知道这不应该是设备问题。但为什么它不适用于我的应用程序?我没有得到任何错误。
我不知道我做错了什么。如果您知道答案或有任何建议,请告诉我!

最佳答案

谷歌 says “以 Android 11 为目标且使用文本转语音的应用应在其 list 的查询元素中声明 TextToSpeech.Engine#INTENT_ACTION_TTS_SERVICE:”
因此,将其添加到您的 AndroidManifest.xml 中:

<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
<application 之前添加这个标签对我有用。 (Android Studio 说 Element queries is not allowed here 虽然)。

关于java - TTS : Speak fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63092645/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com