【问题标题】:why TextToSpeech synthesizeToFile returns -1?为什么 TextToSpeech synthesizeToFile 返回 -1?
【发布时间】:2019-06-28 12:30:53
【问题描述】:

我正在尝试使用 synthesizeToFile 创建一个文件:

TextToSpeech tts = new TextToSpeech(getApplicationContext(), this, "com.google.android.tts");

public void onInit(int status)
{
    if (status == TextToSpeech.SUCCESS)
    {
        String textToGenerate = "this is a test";
        // /data/data/com.domain.my/files is returned by getFilesDir()
        String completePathFile = "/data/data/com.domain.my/files/_12345_test";

        File fileToGenerate = new File(completePathFile);
        String fileName = fileToGenerate.getName();

        // this works on Android 6
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            Bundle bundleTts = new Bundle();
            bundleTts.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);

            tts.synthesizeToFile
            (
                    textToGenerate
                    , bundleTts
                    , fileToGenerate
                    , fileName
            );
        }
        // this doesn't works on Android 4.1: response is -1
        else
        {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, fileName);

            int response = tts.synthesizeToFile
            (
                    textToGenerate
                    , hashMap
                    , completePathFile
            );
            Log.d("testTTS", "Generation file " + fileName + " - response = " + response);
        }
    }
}

对于搭载 Android 6 的设备,synthesizeToFile 方法可以正常工作。

对于具有 Android 4.1 的设备,synthesizeToFile 方法返回 -1

我已经通过 getEngines() 检查了是否安装了“com.google.android.tts”。

如何调试我的脚本以发现为什么synthesizeToFile 返回-1

还有另一种使用 TTS 生成该文件的方法吗?

我需要在内部存储中执行此操作(getFilesDir() 返回的路径),所以我不能请求外部存储权限。

编辑:

在logcat中我发现了这个错误:

E/TextToSpeechService: Can't use /data/data/com.domain.my/files/_12345_test due to exception java.io.IOException: open failed: EACCES (Permission denied)

我已经试过了:

setWritable(true)

setWritable(true, true)

但即使两者都返回true,异常仍然发生。

那么,现在该如何解决呢?

【问题讨论】:

  • 不知道这个 API。它是否也设置了errno? TextToSpeech 类中是否有“最后一个错误”方法?
  • @GemTaylor 我搜索了整个文档 (developer.android.com/reference/android/speech/tts/TextToSpeech) 但不幸的是没有这样的方法:(
  • @KevinCruijssen,我看到了那个注释,但synthesizeToFile 方法返回一个值,该值表示“将请求放入队列”操作的结果,然后异步执行。如果该队列操作失败,它会返回 false 而不尝试合成这些。在文档中:return "ERROR (-1) or SUCCESS (0) of queueing the synthesizeToFile operation"
  • 我建议测试较旧的实现(包含在您的 else {} 子句中)是否适用于最近的 API。如果不是,那么问题出在该代码中。您还可以使用您的 logcat 查看有关返回 -1 的原因的更多详细信息,例如模拟器上的只读文件系统等。此外...我注意到您的 synthesizeToFile 参数对于较旧的实现是不同的(completeFilePath vs文件名)。

标签: java android file debugging text-to-speech


【解决方案1】:

我发现要知道synthesizeToFile返回值-1的原因,我需要在logcat中查看:

E/TextToSpeechService:由于 java.io.IOException 异常,无法使用 /data/data/com.domain.my/files/_12345_test:打开失败:EACCES(权限被拒绝)

现在,我必须知道为什么会出现这个异常......

【讨论】:

  • 你能发布解决方案吗,因为综合阻止我创建文件,为什么错误 -1 请帮助我这里的 android pie
  • 使用路径为--- /storage/emulated/0/RingTone/HEY.mp3 失败
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
  • 2012-01-25
  • 2012-03-05
  • 2017-06-04
  • 2015-01-25
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多