【问题标题】:TextToSpeech android build version not playing audioTextToSpeech android构建版本不播放音频
【发布时间】:2015-01-10 17:00:28
【问题描述】:

我的文字转语音引擎在 android 上有一个奇怪的行为:

当我在 Galaxy S5 上测试文字转语音时,一切正常,音频播放土耳其语和德语。

在其他一些手机(例如一部 LG)上,textToSpeech 也可以工作,但以下情况除外:

  1. 导出应用(构建 apk)并在手机上手动安装
  2. 切换到土耳其语(德语始终有效!)

问题是我没有收到任何错误消息 - TTS 似乎已正常初始化。

任何提示将不胜感激!

这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setLogo(R.drawable.anne_eli_icons_pfeile_zurueck_weiss_17px);
        getActionBar().setHomeButtonEnabled(true);
        textToSpeech = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            log("onInit()");
            int result = textToSpeech.setLanguage(new Locale(getTransLanguage()));
            log("result:" + result);
            textToSpeech.setSpeechRate(1.2f); // set speech speed rate

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Tools.showToast(this, "Language " + getTransLanguage() + " is not supported! Error code: " + result);
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);

            } else {
                speechEnabled = true;
            }

        } else {
            speechEnabled = false;
            Tools.showToast(this, "Text to speech initialization failed!");
        }
    }

【问题讨论】:

  • 说话有回音吗?即使语言可用并且服务已成功初始化,基于网络的 TTS 也可能在 speak() 调用中失败。
  • @alanv speak() 总是返回 "0" :/
  • 您是针对自己的自定义引擎还是其他引擎进行测试?
  • 自定义引擎?我使用“普通”Android TextToSpeech API:developer.android.com/reference/android/speech/tts/…
  • 根据我自己的经验,请确保音量已打开。 TTS 的音量与铃声不同!在 tts 应该说话时按音量上键。

标签: android audio text-to-speech


【解决方案1】:

之前,请检查 TTS 服务是否可用:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;

/**
 * This class demonstrates checking for a TTS engine, and if one is
 * available it will spit out some speak.
*/
public class Main extends Activity implements TextToSpeech.OnInitListener
{
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Fire off an intent to check if a TTS engine is installed
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

}

/**
 * Executed when a new TTS is instantiated. Some static text is spoken via TTS here.
 * @param i
 */
public void onInit(int i)
{
    mTts.speak("Hello folks, welcome to my little demo on Text To Speech.",
            TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
            null);
}


/**
 * This is the callback from the TTS engine check, if a TTS is installed we
 * create a new TTS instance (which in turn calls onInit), if not then we will
 * create an intent to go off and install a TTS engine
 * @param requestCode int Request code returned from the check for TTS engine.
 * @param resultCode int Result code returned from the check for TTS engine.
 * @param data Intent Intent returned from the TTS check.
 */
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == MY_DATA_CHECK_CODE)
    {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
        {
            // success, create the TTS instance
            mTts = new TextToSpeech(this, this);
        }
        else
        {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

/**
 * Be kind, once you've finished with the TTS engine, shut it down so other
 * applications can use it without us interfering with it :)
 */
@Override
public void onDestroy()
{
    // Don't forget to shutdown!
    if (mTts != null)
    {
        mTts.stop();
        mTts.shutdown();
    }
    super.onDestroy();
}

}

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多