我怎样才能让TTS将文件写入我的应用程序目录?[英] How can I allow the TTS to write files to my app's directories?

本文是小编为大家收集整理的关于我怎样才能让TTS将文件写入我的应用程序目录?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想将从TextTospeech引擎的输出写入我的应用程序的缓存目录.为了让TTS写在那里,我首先必须允许它这样做.但是我不知道如何.我知道通常可以通过将FileDescriptor交给FileDescriptor来解决此类问题,从而为访问特定文件提供了权限.但是我不能用tts做到这一点,因为textTospeech.synthesizetofile方法只接受文件路径作为字符串,no FileDescriptor.那该怎么办?

提出我的观点,即TTS确实没有权限写入我的应用程序的目录,这是代码...

TextToSpeech mTts = new new TextToSpeech(context, this);
mTts.synthesizeToFile(text, null, getCacheDir() + "/" + "speech.wav");

调试器日志:

08-20 14:46:11.257: ERROR/TtsService(336): Can't create
/data/data/com.myorg.myapp/cache/speech.wav due to exception java.io.IOException: Permission denied

推荐答案

textTospeech.synthesizetofile()在真实设备中不起作用.它仅适用于avds.

我也一直在使用原始的PICO TTS引擎和第三方TTS引擎进行实验,试图写入SDCARD或内部内存(在扎根的设备上):

context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);

但是,正如您指出的那样,该方法返回TextToSpeech.SUCCESS而无需实际创建文件.

如果您必须将TTS输出记录到WAV文件,请将耳机输出连接到PC中的声卡中的AUX输入并使用任何录制软件来捕获.

其他推荐答案

textTospeech.synthesizetofile()实际上确实在真实设备上工作,尽管有点螺丝中. 我遇到了同样的问题,但最终使它起作用.我使用的代码是:

speakTextTxt = speakText.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_SAVE_ID);

String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/sounds/");
appTmpPath.mkdirs();
String tempFilename = "tmpaudio.wav";
tempDestFile = appTmpPath.getAbsolutePath() + "/" + tempFilename;

mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);

但是,今天我发现了其他奇怪的东西.我切换到另一台计算机进行开发,然后开始再次获得相同的错误" Java.io.io.ioexception:拒绝权限". 然后,我进入了Android SDK Manager,并确保所有" SDK平台"和" Goole Inc.的Google Apis".为所有Android版本安装了包装.我不确定哪个软件包版本已修复,但它又可以使用. 因此,显然您可以拥有完美的代码,但是您的开发环境可能会出现问题,这可能会导致问题. 我希望这可以帮助某人.

本文地址:https://www.itbaoku.cn/post/102389.html

问题描述

I want to write output from the TextToSpeech engine to my app's cache directory. In order to for the TTS to write there I first have to give it permissions to do so. But I don't know how. I understand that normally such problems can be solved by handing a FileDescriptor over thus giving permissions to access a specific file. But I can't do that with TTS, as the TextToSpeech.synthesizeToFile method only accepts the file path as a String, no FileDescriptor. So what to do?

To make my point that TTS really hasn't permissions to write to my app's directories, here's the code...:

TextToSpeech mTts = new new TextToSpeech(context, this);
mTts.synthesizeToFile(text, null, getCacheDir() + "/" + "speech.wav");

And the debugger log:

08-20 14:46:11.257: ERROR/TtsService(336): Can't create
/data/data/com.myorg.myapp/cache/speech.wav due to exception java.io.IOException: Permission denied

推荐答案

TextToSpeech.synthesizeToFile() doesn't work in real devices. It only works in AVDs.

I have been experimenting with this, too, using both the original Pico TTS engine and a third party TTS engine, trying to write to either the sdcard or the internal memory (on a rooted device):

context.getDir("soundfiles", Context.MODE_WORLD_WRITEABLE);

But, as you noted, the method returns TextToSpeech.SUCCESS without actually creating the file.

If you must record your TTS output to a WAV file, connect the headset output to the aux input in a sound card in your PC and use any recording software to capture that.

其他推荐答案

TextToSpeech.synthesizeToFile() does actually work on real devices, though it's a bit screwy. I was having the same issue but eventually got it working. The code I use is:

speakTextTxt = speakText.getText().toString();
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_SAVE_ID);

String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/sounds/");
appTmpPath.mkdirs();
String tempFilename = "tmpaudio.wav";
tempDestFile = appTmpPath.getAbsolutePath() + "/" + tempFilename;

mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);

However, today I found something else quite strange. I switched to a different computer to do development on and then started getting the same error again "java.io.IOException: Permission denied". I then went into the Android SDK Manager and made sure all "SDK Platform" and "Google APIs by Goole Inc." packages were installed for all Android versions. I'm not sure exactly which package version fixed it but it's working again. So apparently you can have perfectly fine code but something might be off with your development environment that can cause issues. I hope this can help someone.