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 に答える