IBM语音到文本-如何将MP3音频文件转换为字节数组以发送到IBM Watson中的服务器,以使用语音到文本API将音频转换为文本
答案 0 :(得分:0)
您可以像这样将mp3转换为字节数组;
fun convert(context: Context): ByteArray {
val outputFile = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS).absolutePath + "/sample.mp3"
var soundBytes = ByteArray(0)
try {
val inputStream = context.contentResolver.openInputStream(Uri.fromFile(File(outputFile)))
soundBytes = IOUtils.toByteArray(inputStream)
} catch (e: Exception) {
e.printStackTrace()
}
return soundBytes
}
Java
public byte[] convert(Context context) {
String outputFile =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/sample.mp3";
byte[] soundBytes = new byte[0];
try {
InputStream inputStream =
context.getContentResolver().openInputStream(Uri.fromFile(new File(outputFile)));
soundBytes = new byte[inputStream.available()];
soundBytes = IOUtils.toByteArray(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
return soundBytes;
}