これは、オーディオ サンプルを録音しようとするコードです。ただし、サンプル レートがわからないため、作成されていない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);
}
}
これで、オーディオサンプルを取得せずにオーディオフォーマットオブジェクトを構築するにはどうすればよいですか?