在开始录制之前,我是否必须猜测样本的采样率?

时间:2011-08-05 10:51:34

标签: java audio record javasound

这是一个尝试录制音频示例的代码:但我没有构建AudioFormat对象(已传递给DataLine.Info因为我不知道采样率。

修改

我看到只是随机放置8000的采样率。但它没事吗?我可以保留任何采样率值吗?

boolean lineIsStopped = false;
TargetDataLine line = null; 
AudioFormat af; // object not constructed through out
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af); // af not initialized
try {
  line = (TargetDataLine)AudioSystem.getLine(info); 
  line.open( af );      
} catch( LineUnavailableException ex ) {
   // handle the error
  }

// now we are ready for an input
// call start to start accepting data from mic
byte data[] = new byte[ line.getBufferSize() / 5 ];
 line.start(); // this statement starts delivering data into the line buffer
// start retreiving data from the line buffer
 int numBytesRead;
 int offset = 0;
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 while( ! lineIsStopped ) { // when the line is not stopped i.e is active
     numBytesRead = line.read( data , offset , data.length );
     // now save the data
     try {
        out.write(data); // writes data to this output stream !
     } catch( Exception exc) {
         System.out.println(exc);
       }
 }

在这个如何构建音频格式对象而不获取任何音频样本?

1 个答案:

答案 0 :(得分:2)

阅读完评论后,您正在录制麦克风。在这种情况下,您需要根据麦克风所需的音质设置音频格式。如果你想要8kh的电话质量就好了。如果你想要磁带质量22khz,如果你想要CD质量音频44.1khz。当然,如果你通过网络传输,那么8khz可能就足够了。

如果你的应用程序让用户可以控制他们想要的质量,那么将它作为一个设置总是一个好主意。