我的代码有问题,我想将 mp4 音频文件转换为字节数组。例如,我有 2 个包含不同歌曲但长度相同的音频文件,但是当我打印字节数组时,结果总是显示相似,这是我的代码:
try {
FileInputStream fin = null;
fin = new FileInputStream("/mnt/sdcard/audiorecorder/tes.mp4");
BufferedInputStream bis = new BufferedInputStream(fin);
DataInputStream dis = new DataInputStream(bis);
byte fileContent[] = toByteArray(dis) ;
Log.d("tes", Arrays.toString(fileContent));
}
catch (Exception e){}
}
public static byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(in,out);
return out.toByteArray();
}
private static long copy(InputStream from, OutputStream to) throws IOException {
// TODO Auto-generated method stub
byte[] buf = new byte[4000];
long total = 0;
while (true){
int r = from.read(buf);
if (r==-1) break;
to.write(buf,0,r);
total += r;
}
return total;
}