各位大侠,小弟现在弄一个麦克风录音..想输出它的PCM编码,但是,怎么弄输出的都好像不是..麻烦各位大虾帮忙看看...给点建议或见解也行..不胜感激!!!
   下面是我的代码,用 java.Sound  做的.
private AudioFormat audioFormat = null;
private TargetDataLine targetDataLine = null;
private DataLine.Info dataLine_info = null;
private ByteArrayInputStream streamIn = null;
private ByteArrayOutputStream streamOut = null;
private DataLine.Info info = null;
private SourceDataLine sdl = null;
private AudioInputStream m_audioInputStream = null;
public   test1() throws LineUnavailableException, IOException {

audioFormat = new AudioFormat(44100.0f, 8, 1, true, false);
dataLine_info = new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLine_info);
byte[] b = new byte[100000]; streamOut = new ByteArrayOutputStream();
targetDataLine.open(audioFormat);
targetDataLine.start();

int i = targetDataLine.read(b, 0, b.length);
for(int j=0;j<i;j++){
if(b[j]>0){
System.out.println(b[j]+".........."+j);            //我原本认为这里输出的就是PCM编码,但好像不是.....
}
}
if (i > 0) {
streamOut.write(b, 0, i);
}

streamOut.close();
info = new DataLine.Info(SourceDataLine.class, audioFormat);
sdl = (SourceDataLine) AudioSystem.getLine(info);
streamIn = new ByteArrayInputStream(streamOut.toByteArray());

sdl.open();
sdl.start();
i = streamIn.read(b, 0, b.length);
while (i != -1) {
sdl.write(b, 0, i);
streamIn.read(b, 0, b.length);
}
int nBytesRead = 1;
try {
m_audioInputStream = AudioSystem.getAudioInputStream(streamIn);
 m_audioInputStream.read(b, 0, b.length);

} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
targetDataLine.close();
sdl.close();
System.exit(0);

解决方案 »

  1.   

    PCM_SIGNED
    你写得不对,还没录入声音
      

  2.   

    转帖,加点注释/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package test;import java.io.BufferedInputStream;
    import java.io.IOException;import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.TargetDataLine;public class MySound5_1 {
    /**音频格式*/
    private AudioFormat audioFormat = null;
    /**麦克风 输入设备*/
    private TargetDataLine targetDataLine = null;
    /**输入设备*/
    private SourceDataLine sourceDataLine = null;
    /**输入混音器的信息*/
    private DataLine.Info dataLine_info = null;
    /**输出混音器的信息*/
    private DataLine.Info out_dataLine_info = null;public MySound5_1() throws LineUnavailableException, IOException{
       audioFormat = new AudioFormat(8000.0f,8,1,true,false);//设置格式
       dataLine_info = new DataLine.Info(TargetDataLine.class,audioFormat);//得到默认输入混音器的信息
       targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLine_info);//得到输入设备 麦克风   out_dataLine_info = new DataLine.Info(SourceDataLine.class,audioFormat);//
       sourceDataLine = (SourceDataLine)AudioSystem.getLine(out_dataLine_info);//   byte[] b = new byte[100000];
       targetDataLine.open(audioFormat);
       targetDataLine.start();   sourceDataLine.open();
       sourceDataLine.start();   int len = 0;
       while((len = targetDataLine.read(b, 0, b.length)) > 0){//从输入设备读声音 麦克风
        sourceDataLine.write(b, 0, len);//同时向输出设备送声音 喇叭
       }   targetDataLine.close();
       sourceDataLine.close();
       System.exit(0);
    }public static void main(String[] args) {
       try {
        new MySound5_1();
       } catch (LineUnavailableException e) {
        e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
       }
    }
    }
      

  3.   

    各位谢谢了...不过输出的PCM数据还是不大对.你们帮忙看看这个采样的信息如下(一样的) : PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endianAudioInputStream m_audioInputStream = AudioSystem.getAudioInputStream(file);
    int nBytesRead = 1;
    byte[] abData = new byte[16000];
    nBytesRead = m_audioInputStream.read(abData, 0, abData.length);
    nBytesRead 输出之后.是 4608 另外一个,
    TargetDataLine target_line = null;
    AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
    fFrameRate, 16, 2, 4, fFrameRate, false);
    DataLine.Info lineInfo = new DataLine.Info(TargetDataLine.class,
    format, 65536);
    target_line = (TargetDataLine) AudioSystem.getLine(lineInfo);AudioInputStream audio_input = new AudioInputStream(target_line); 然后byte[] b = new byte[16000];
    int  nBytesRead = 1;

      nBytesRead = audio_input.read(b, 0, b.length); nBytesRead 输出之后 是 16000 ...小弟苦想多日.不得其果...还往各位高手帮帮忙..感激不尽~~