首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android文本到语音转换

Android文本到语音转换
EN

Stack Overflow用户
提问于 2016-11-30 11:06:02
回答 2查看 1.2K关注 0票数 1

我正在尝试使用TextToSpeech类在我的应用程序中说出文本。当我运行我的代码时,我听不到任何声音,音量很大。我的代码出了什么问题?我需要许可或其他什么吗?

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener  {

    TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textToSpeech = new TextToSpeech(this, this);

        speakOut();
    }

    @Override
    public void onInit(int Text2SpeechCurrentStatus) {

        if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {

            int result = textToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

    private void speakOut() {
        String g= "Hello";
        textToSpeech.speak(g, TextToSpeech.QUEUE_FLUSH, null);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-11-30 13:35:39

首先也是最重要的一件事:检查设备中是否安装了TTS引擎。

不,你不需要任何许可就可以使用TTS。

在活动的onCreate()方法中初始化TextToSpeech实例,如下所示。

代码语言:javascript
复制
TextToSpeech t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
     @Override
     public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
           t1.setLanguage(Locale.UK);
        }
     }
  });

//这是您的speakOut()方法。

代码语言:javascript
复制
   private void speakOut() {
    String g= "Hello";
     t1.speak(g, TextToSpeech.QUEUE_FLUSH, null);
}

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2016-11-30 11:59:20

这可能是一个评论,我不确定,但您可以尝试更改

if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {

转到

if (Text2SpeechCurrentStatus != TextToSpeech.ERROR) {

也许你可以调试,Text2SpeechCurrentStatus的价值是什么?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40879552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档