Android 如何播放.WAV向后?
我有.WAV文件。我需要倒着玩。我怎么能做到?我使用InputStream从文件中读取数据,然后将其转换为字节数组。反转此数组,然后将此数组写入另一个文件?哪里是错误的逻辑Android 如何播放.WAV向后?,android,Android,我有.WAV文件。我需要倒着玩。我怎么能做到?我使用InputStream从文件中读取数据,然后将其转换为字节数组。反转此数组,然后将此数组写入另一个文件?哪里是错误的逻辑 public void copyWaveFile(String inFilename,String outFilename){ FileInputStream in = null; FileOutputStream out = nul
public void copyWaveFile(String inFilename,String outFilename){
FileInputStream in = null;
FileOutputStream out = null;
long totalAudioLen = 0;`enter code here`
long totalDataLen = totalAudioLen + 36;
long longSampleRate = frequency;
int channels = 1;
long byteRate = RECORDER_BPP * frequency * channels/8;
byte[] data = new byte[recBufSize];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
// Why this is don't work?
data = convertStreamToByteArray(in,recBufSize);
byte[] reverseData = reverseByteArray(data);
out.write(reverseData);
WriteWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// convert InputStream to byte array
public static byte[] convertStreamToByteArray(InputStream is,int size) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[size];
byte[] result = null;
int b;
try {
while ((b = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, b);
}
result = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
// reverse byte array
private byte[] reverseByteArray(byte[] arr) {
int i = 0, j = arr.length-1;
byte tmp;
while(i<j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
return arr;
}
// header for wav file`enter code here
private void WriteWaveFileHeader(
FileOutputStream out, long totalAudioLen,
long totalDataLen, long longSampleRate, int channels,
long byteRate) throws IOException {
byte[] header = new byte[44];
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;`enter code here`
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (1 * 16 / 8); // block align
header[33] = 0;
header[34] = RECORDER_BPP; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
out.write(header, 0, 44);
}
public void copyWaveFile(字符串inFilename,字符串outFilename){
FileInputStream in=null;
FileOutputStream out=null;
long totalAudioLen=0;`在此处输入代码`
long totalDataLen=totalAudioLen+36;
长时间采样=频率;
int通道=1;
long byteRate=记录器_BPP*频率*通道/8;
字节[]数据=新字节[recBufSize];
试一试{
in=新文件输入流(inFilename);
out=新文件输出流(outFilename);
totalAudioLen=in.getChannel().size();
totalDataLen=totalAudioLen+36;
//为什么这不起作用?
数据=convertStreamToByteArray(in,recBufSize);
字节[]reverseData=reverseByteArray(数据);
输出。写入(反向数据);
WriteWaveFileHeader(out、totalAudioLen、totalDataLen、longSampleRate、Channel、byteRate);
in.close();
out.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
//将InputStream转换为字节数组
公共静态字节[]convertStreamToByteArray(InputStream为,int size){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]缓冲区=新字节[大小];
字节[]结果=空;
int b;
试一试{
而((b=is.read(buffer,0,buffer.length))!=-1){
写入(缓冲区,0,b);
}
结果=baos.toByteArray();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
baos.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回结果;
}
//反向字节数组
专用字节[]反向字节数组(字节[]arr){
int i=0,j=arr.length-1;
字节tmp;
而(i>8)&0xff);
头[6]=(字节)((totalDataLen>>16)和0xff);
头[7]=(字节)((totalDataLen>>24)和0xff);
标题[8]=“W”;
标题[9]=“A”;
标题[10]=“V”;
标题[11]=“E”;
标题[12]=“f”;/“fmt”块
标题[13]=“m”;
标题[14]=“t”;
标题[15]='';
header[16]=16;//4字节:“fmt”块的大小
标题[17]=0;
标题[18]=0;
标题[19]=0;
头[20]=1;//格式=1
header[21]=0;`在此处输入代码`
头[22]=(字节)通道;
标题[23]=0;
头文件[24]=(字节)(longSampleRate&0xff);
头[25]=(字节)((长采样>>8)和0xff);
头[26]=(字节)((长采样>>16)和0xff);
头[27]=(字节)((长采样>>24)和0xff);
头[28]=(字节)(字节和0xff);
头[29]=(字节)((字节>>8)和0xff);
头[30]=(字节)((字节>>16)和0xff);
头[31]=(字节)((字节>>24)和0xff);
头[32]=(字节)(1*16/8);//块对齐
标题[33]=0;
头[34]=记录器\u BPP;//每个样本的位
标题[35]=0;
标题[36]=“d”;
标题[37]=“a”;
标题[38]=“t”;
标题[39]=“a”;
标题[40]=(字节)(totalAudioLen&0xff);
头[41]=(字节)((totalAudioLen>>8)和0xff);
头[42]=(字节)((totalAudioLen>>16)和0xff);
头[43]=(字节)((totalAudioLen>>24)和0xff);
out.write(头,0,44);
}
它取决于比特率。但一般来说,您不能只对字节进行重新排序。wave文件有其逻辑格式,必须保留该格式才能再次播放该文件。例如,对于16位的PCM编码波形文件,我会尝试反转16位块。因此,最后一个16位块将首先执行,而不会对字节进行重新排序。想一想,我怀疑这是否可行。必须有一种头,必须在开始时保留或重写,可能还有一些字节表示音频数据的结束
这应该会有帮助,但并不像你想象的那么琐碎。
也许有一个Java库可以实现这一点?谢谢,这是一个对我有用的链接。如果有帮助,这里有一个关于如何在C#中反转.wav文件的教程。应该很容易转换成Java:非常感谢!我会朝这个方向搜索。