在我的应用程序中,我想将原始字节数据更改为音频。因此,首先我接收数据并将其添加到字节数组列表中,然后我想将整个数组列表更改为音频。由于数据是通过音频插孔获取的原始数据,如果您让我知道哪种音频格式合适(wav、3gp 等),我将不胜感激。另外,我在将这些数据保存在文件中时遇到问题。这是我的代码。在 fileOuputStream.write( audiod.toArray()) 行中,它给出一个错误并要求我将 audiod 的类型更改为 byte 。
private static ArrayList<Byte> audiod = new ArrayList<Byte>();
audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.wav";
if (dg == 1) {
//audiod.append(data[i]);
for (int i = 0; data != null && i < data.length; i++) {
audiod.add(data[i]);
}
if (stt.equalsIgnoreCase(("r"))) {
dg = 0;
recordButton.setEnabled(true);
File file = new File(audioFilePath);
try {
FileOutputStream fileOuputStream = new FileOutputStream(audioFilePath);
fileOuputStream.write( audiod.toArray());
fileOuputStream.close();
System.out.println("Done");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
最佳答案
audiod.toArray()
将为您提供一个 Byte[]
,但是您需要一个 byte[]
。在 Java 中,有所谓的原始数据类型的包装类。 Byte
是一个保存 byte
值的对象。
为什么要使用Byte
列表?它需要比您似乎拥有的简单的 byte[]
更多的内存(data
)。
数据格式(wav、3gp 等)取决于您获取数据的方式。
关于java - 将原始字节数据转换为音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24847654/