[英]Converting raw byte data to audio
In my application, I want to change the raw byte data to the audio. 在我的应用程序中,我想将原始字节数据更改为音频。 So, at first I receive the data and add it to a byte array list and than I want to change the whole array list to the audio. 因此,首先,我接收到数据并将其添加到字节数组列表中,然后我想将整个数组列表更改为音频。 As the data is raw through audio jack, I would appreciate if you let me know which audio format is suitable (wav,3gp and so on)? 由于数据是通过音频插孔输出的,如果您让我知道哪种音频格式合适(wav,3gp等),将不胜感激。 Also, I have a problem with saving this data in a file. 另外,我在将这些数据保存到文件中时遇到问题。 This is my code. 这是我的代码。 in line of fileOuputStream.write( audiod.toArray()), it give an error and asked me to change the type of audiod to byte. 在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()
will give you a Byte[]
, however you need a byte[]
. audiod.toArray()
将为您提供Byte[]
,但是您需要一个byte[]
。 In Java there are so called wrapper classes for the primitive data types. 在Java中,原始数据类型有所谓的包装器类。 A Byte
is an object that holds byte
values. Byte
是保存byte
值的对象。
Why do you use a list of Byte
at all? 为什么要使用Byte
列表? It needs a lot more memory than a simple byte[]
you seem to have anyhow ( data
). 它比您似乎拥有的简单byte[]
( data
)需要更多的内存。
The data format (wav,3gp and so on) depends on how you got your data. 数据格式(wav,3gp等)取决于您如何获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.