How do I make the code fill in the onResponse()
the result of the transcript instead of always changing it in responde.setText(output)
?
I know that the setText within the function will always replace the text, but what better way to get the transcription result to be concatenated to the end result?
An example of how I expect to work is like the Google Translate website.
The code is below:
package Try_Google_Speech_Recognition_Simple;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.recognizer.GSpeechDuplex;
import com.darkprograms.speech.recognizer.GSpeechResponseListener;
import com.darkprograms.speech.recognizer.GoogleResponse;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import net.sourceforge.javaflacencoder.FLACFileWriter;
public class TryGoogleSpeechRecognitionSimple implements GSpeechResponseListener {
public static void main(String[] args) throws IOException {
final Microphone mic = new Microphone(FLACFileWriter.FLAC);
//Don't use the below google api key , make your own !!! :)
GSpeechDuplex duplex = new GSpeechDuplex("AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw"); //default
duplex.setLanguage("PT-br");
JFrame frame = new JFrame("DEMO Jarvis Speech API");
frame.setDefaultCloseOperation(3);
JTextArea response = new JTextArea();
response.setEditable(true);
response.setWrapStyleWord(true);
response.setLineWrap(true);
final JButton record = new JButton("Gravar");
final JButton stop = new JButton("Parar");
stop.setEnabled(false);
record.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Thread(() -> {
try {
duplex.recognize(mic.getTargetDataLine(), mic.getAudioFormat());
} catch (Exception ex) {
ex.printStackTrace();
}
}).start();
record.setEnabled(false);
stop.setEnabled(true);
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
mic.close();
duplex.stopSpeechRecognition();
record.setEnabled(true);
stop.setEnabled(false);
}
});
JLabel infoText = new JLabel("<html><div style=\"text-align: center;\">Gravação<center></html>", 0);
frame.getContentPane().add(infoText);
infoText.setAlignmentX(0.5F);
JScrollPane scroll = new JScrollPane(response);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), 1));
frame.getContentPane().add(scroll);
JPanel recordBar = new JPanel();
frame.getContentPane().add(recordBar);
recordBar.setLayout(new BoxLayout(recordBar, 0));
recordBar.add(record);
recordBar.add(stop);
frame.setVisible(true);
frame.pack();
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
duplex.addResponseListener(new GSpeechResponseListener() {
String texto_atual = "";
String output;
public void onResponse(GoogleResponse gr) {
output = gr.getResponse();
this.texto_atual = response.getText();
response.setText(output);
}
});
}
@Override
public void onResponse(GoogleResponse paramGoogleResponse) {
// TODO Auto-generated method stub
//System.out.println(paramGoogleResponse.getResponse());
}
}