【发布时间】:2017-08-15 05:23:27
【问题描述】:
我正在尝试将 wit.ai 语音转换为文本,这样我就可以将录制的音频从 whatsapp 转换为文本,问题是我不断出错。
wit.ai 支持 Content-Type 标头的“audio/wav”、“audio/mpeg3”、“audio/ulaw”和“audio/raw”。如果使用音频/原始文件,您还必须提供以下参数:编码、位、速率和字节序
我从 whatsapp 获得的文件是 .ogg。
由于他们不支持这种文件,我尝试了所有可能变化的音频/原始文件,但我一直收到同样的错误:
{
"error" : "Bad request",
"code" : "bad-request"
}
当我尝试不同的类型(例如音频/wav)时,我得到一个不同的错误,这是有道理的:
{
"error" : "Mismatch between the provided content-type and the bytes you sent.\nAre you sure it's a valid sound file?",
"code" : "content-type-mismatch"
}
为了获取我正在做的文件:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.w("myapp", "got the intent");
if (Intent.ACTION_SEND.equals(action) && type != null) {
audioUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
为了阅读我正在做的文件:
InputStream inputStream = getContentResolver().openInputStream(uri);
String type = getContentResolver().getType(uri);
Log.w("myapp", "type of file: " + type);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream fis = getContentResolver().openInputStream(uri);
try {
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
Log.w("myapp", Integer.toString(baos.toByteArray().length));
return baos.toByteArray();
我做错了什么?
【问题讨论】:
标签: android audio speech-to-text wit.ai