在流媒体音乐时,我得到了short[]类型的pcm数据,我想把它保存到我的安卓设备中的文件中,这样我以后就可以再次播放它(使用AudioTrack)。我不希望音乐的存储在内存和cpu上是有效的。
如何将short[]保存到文件中,因为我看不到任何函数in.write(short[])?
如何减少用于保存此文件的\cpu空间?
发布于 2011-11-24 17:40:25
用DataOutputStream包装你的FileOutputStream:
DataOutputStream doStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream));
doStream.writeInt(numberArray.length); //Save size
for (int i=0;i<numberArray.length;i++) {
doStream.writeShort(numberArray[i]); //Save each number
}回读的方法也是一样:
DataInputStream diStream = new DataInputStream(new BufferedInputStream(fileInputStream));
int size = diStream.readInt(); //Read size
short[] data = new short[size]; //Create new array with required length
for (int i=0;i<size;i++) {
data[i] = diStream.readShort(); //Read each number
}发布于 2011-11-24 17:49:10
不需要对MP3或类似的代码进行任何编码,您总是可以这样做。
short[] sound = ...;
ByteBuffer byteMyShorts = ByteBuffer.allocate(sound.length * 2);
ShortBuffer shortBytes = byteMyShorts.asShortBuffer();
shortBytes.put(sound);
byteMyShorts.flip();
// byteMyShorts.array() now contains your short[] array as an
// array of bytes.https://stackoverflow.com/questions/8254804
复制相似问题