Android TextToSpeech 总是无法运行。

huangapple go评论90阅读模式
英文:

Android TextToSpeech always fails to Run

问题

出于某种原因,尽管我已验证所需的数据文件可用,但```onInit()```始终以错误代码被调用。

我已尝试启动从教程中完整复制的TTS(文本转语音)代码,但也不起作用。

以下是我的初始化代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == DATA_CHECK_SUCCESS) {

        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            textToSpeech = new TextToSpeech(this, this);
        }

        else {

            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);

        }

    }

}

@Override
public void onInit(int initStatus) {

    if (initStatus == TextToSpeech.SUCCESS) {
        if(textToSpeech.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
            textToSpeech.setLanguage(Locale.US);
    }
    else if (initStatus == TextToSpeech.ERROR) {
        Toast.makeText(this, "抱歉!文本转语音失败...", Toast.LENGTH_LONG).show();
    }

}



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Intent TTSIntent = new Intent();
    TTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(TTSIntent, DATA_CHECK_SUCCESS);

}

提前致谢。
英文:

For some reason onInit() always gets called with an error code despite the fact that I have verified that the required data files are available.

I have tried launching TTS(text-to-speech) code which has been copied in it's entirety from a tutorial and that does not work either

The following is my initialization code:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == DATA_CHECK_SUCCESS) {

            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                textToSpeech = new TextToSpeech(this, this);
            }

            else {

                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);

            }

        }

    }

    @Override
    public void onInit(int initStatus) {

        if (initStatus == TextToSpeech.SUCCESS) {
            if(textToSpeech.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                textToSpeech.setLanguage(Locale.US);
        }
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }

    }

   

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Intent TTSIntent = new Intent();
        TTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(TTSIntent, DATA_CHECK_SUCCESS);

    }

Thanks in advance

答案1

得分: 1

我之前遇到过类似的问题,与语音转文字有关。问题是设备上安装的Google应用被禁用或没有麦克风权限。

看看这是否有助于解决你的问题。

英文:

I have faced a similar issue before with Speech to Text, the problem was that the Google app installed on the device was disabled or does not have the microphone permission.

Check if this helps with your issue.

答案2

得分: 0

这是Android Studio提供的Android模拟器中的问题,因为它不提供TTS。

英文:

Turns out that this is an issue in the Android emulator provided with Android studio as it does not provide TTS

huangapple
  • 本文由 发表于 2020年7月30日 05:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63162688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定