web-dev-qa-db-ja.com

Java

Javaアプリケーションを使用して音声を録音したい。これは基本的にクライアント側で実行されるアプレットになると思いますが、その方法がわかりません...また、録音した音声を再生したいです。

Java Speech APIについて聞いたことがあります。それが役立つかどうか、何か考えはありますか?

13
Varun

パーティーに遅れましたが、オーディオのキャプチャに関する公式ドキュメントは次のとおりです。 http://docs.Oracle.com/javase/tutorial/sound/capturing.html

(そして、ここの上のリンクから直接コピーされたのは、それを行うためのサンプルコードです:)

TargetDataLine line;
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
    // Handle the error ...

}
// Obtain and open the line.
try {
    line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
} catch (LineUnavailableException ex) {
    // Handle the error ...
}

// Assume that the TargetDataLine, line, has already
// been obtained and opened.
ByteArrayOutputStream out  = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize() / 5];

// Begin audio capture.
line.start();

// Here, stopped is a global boolean set by another thread.
while (!stopped) {
    // Read the next chunk of data from the TargetDataLine.
    numBytesRead =  line.read(data, 0, data.length);
    // Save this chunk of data.
    out.write(data, 0, numBytesRead);
}
7
xbakesx