1

这是一个将尝试记录音频样本的代码:但我有一个未构造的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);
       }
 }

在这种情况下,我如何在不获取任何音频样本的情况下构造音频格式对象?

4

1 回答 1

2

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

如果您的应用程序将其作为一个设置,这样用户就可以控制他们想要的质量,这总是一个好主意。

于 2011-08-05T13:08:28.343 回答