我做了一個小應用程序,可以從我的電腦上捕捉聲音。系統的簡單過程是當我播放一個音樂文件時,它會捕捉聲音,當我播放捕捉到的東西時,它會播放。完全一切正常。現在我想要做的就是打印播放。假設我捕捉到「嗨,早上好」這樣的聲音,現在當我按下回放時,它應該不得不用文本打印錄製的內容。捕獲和回放編碼如下。如何在音頻文件中打印該詞
private void captureAudio() {
try {
final AudioFormat format = getFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
out = new ByteArrayOutputStream();
running = true;
try {
while (running) {
int count = line.read(buffer, 0, buffer.length);
if (count > 0) {
out.write(buffer, 0, count);
}
}
out.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-1);
}
}
};
Thread captureThread = new Thread(runner);
captureThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
}
}
private void playAudio() {
try {
byte audio[] = out.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
final AudioFormat format = getFormat();
final AudioInputStream ais = new AudioInputStream(input, format, audio.length/format.getFrameSize());
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
try {
int count;
while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
if (count > 0) {
line.write(buffer, 0, (char)count);
System.out.print((char)count);
}
}
line.drain();
line.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-3);
}
}
};
Thread playThread = new Thread(runner);
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
}
}
請有人建議/幫助我克服這一點。
謝謝。
1)你想要什麼是語音識別,但我懷疑將這些信息放入生成的音頻文件中是可能的。 2)在將它們應用於問題之前,請*閱讀*標籤彈出窗口,這與'音頻流式傳輸'無關。 –
大概看看[API](http://docs.oracle.com/cd/E17802_01/products/products/java-media/speech/forDevelopers/jsapi-doc/)和[Sphinx-4](http:///cmusphinx.sourceforge.net/sphinx4/)。 – NINCOMPOOP
@AndrewThompson - 感謝評論老兄。我已經做了語音識別部分,並且100%正常工作。但在這裏的事情是沒有講話的一部分。它關於播放音頻。要識別語音,應該有一些輸入到目標數據行。這就是我目前所困擾的一點。任何想法? – maXfenda