我想在Android eclipse中以pcm格式在.wav文件中获取语音记录。我在sdcard0中看到了数据,但我听不到。所以我想知道我是否得救了。我读到我应该添加块,即:RIFF标题,FMT和DATA块。如何添加?如果有人知道我该怎么做,我会很感激。请给我一些提示。谢谢。
public static final int FREQUENCY = 44100;
public static final int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_CONFIGURATION_MONO;
public static final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
public String folder_main = "Rize.wav";
及其代码如下;
enter coprivate void recordSound(){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ folder_main);
// Delete any previous recording.
if (file.exists())
file.delete();
try {
file.createNewFile();
// Create a DataOuputStream to write the audio data into the saved file.
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio.
int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
isRecording = true;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++)
dos.writeShort(buffer[i]);
}
audioRecord.stop();
audioRecord.release();
dos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}de here
答案 0 :(得分:1)
我有类似的需求,发现了这个,并且它有效:
下面的方法将您的pcmdata作为短数组,为wav添加正确的标头(基于您的采样率,通道数和位格式),并写入dataoutputstream(文件)。
ex // PCMtoFile(myDataOutStream,myPCMShortArray,44100,2,16);
public static void PCMtoFile(OutputStream os, short[] pcmdata, int srate, int channel, int format) throws IOException {
byte[] header = new byte[44];
byte[] data = get16BitPcm(pcmdata);
long totalDataLen = data.length + 36;
long bitrate = srate * channel * format;
//Marks the file as a riff file. Characters are each 1 byte long.
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
//Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation.
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
//File Type Header. For our purposes, it always equals "WAVE".
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
//Format chunk marker. Includes trailing null
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
//Length of format data as listed above
header[16] = (byte) format;
header[17] = 0;
header[18] = 0;
header[19] = 0;
//Type of format (1 is PCM) - 2 byte integer
header[20] = 1;
header[21] = 0;
//Number of Channels - 2 byte integer
header[22] = (byte) channel;
header[23] = 0;
// Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT).
//Sample Rate = Number of Samples per second, or Hertz.
header[24] = (byte) (srate & 0xff);
header[25] = (byte) ((srate >> 8) & 0xff);
header[26] = (byte) ((srate >> 16) & 0xff);
header[27] = (byte) ((srate >> 24) & 0xff);
// (Sample Rate * BitsPerSample * Channels) / 8.
header[28] = (byte) ((bitrate / 8) & 0xff);
header[29] = (byte) (((bitrate / 8) >> 8) & 0xff);
header[30] = (byte) (((bitrate / 8) >> 16) & 0xff);
header[31] = (byte) (((bitrate / 8) >> 24) & 0xff);
//(BitsPerSample * Channels) / 8.1 - 8 bit mono2 -
//8 bit stereo/16 bit mono4 - 16 bit stereo
header[32] = (byte) ((channel * format) / 8);
header[33] = 0;
//Bits per sample
header[34] = 16;
header[35] = 0;
//"data" chunk header. Marks the beginning of the data section.
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
// Size of the data section.
header[40] = (byte) (data.length & 0xff);
header[41] = (byte) ((data.length >> 8) & 0xff);
header[42] = (byte) ((data.length >> 16) & 0xff);
header[43] = (byte) ((data.length >> 24) & 0xff);
//write the byte form header
os.write(header, 0, 44);
//write the byte form data
os.write(data);
//close the output stream
os.close();
}