|
|
VoskHelper cn.unicoder.myapplication E 文件读取或发送失败: /data/user/0/cn.unicoder.myapplication/files/test16k.wav: open failed: ENOENT (No such file or directory
根据日志信息,问题在于测试文件不存在。/data/user/0/cn.unicoder.myapplication/files/test16k.wav 路径下的文件未找到。
问题分析
测试代码设置了文件路径为应用私有目录下的 test16k.wav
但该文件实际上并不存在,导致 VoskHelper.sendFile() 方法在尝试读取文件时抛出 FileNotFoundException
解决方案
需要将测试音频文件从 assets 目录复制到应用私有目录中:
- private String getTestWavFilePath() {
- Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
- String testFilePath = context.getFilesDir().getAbsolutePath() + "/test16k.wav";
-
- // 检查文件是否存在,如果不存在则从assets复制
- File testFile = new File(testFilePath);
- if (!testFile.exists()) {
- try {
- AssetManager assetManager = context.getAssets();
- InputStream inputStream = assetManager.open("test16k.wav");
-
- FileOutputStream outputStream = new FileOutputStream(testFilePath);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer)) > 0) {
- outputStream.write(buffer, 0, length);
- }
- inputStream.close();
- outputStream.close();
-
- Log.d(tag, "测试文件已复制到: " + testFilePath);
- } catch (IOException e) {
- Log.e(tag, "复制测试文件失败", e);
- }
- }
-
- return testFilePath;
- }
复制代码 记得在文件顶部添加导入语句:
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
复制代码
|
|