Java 语音聊天机器人(百度语音API)(当前预置问答,可用图灵机器人框架扩展)

系列文章:
Java 语音记录(录音,存储为WAV文件):https://blog.csdn.net/haoranhaoshi/article/details/87888382
Java 语音识别(百度语音API):https://blog.csdn.net/haoranhaoshi/article/details/87888407
Java 语音合成并播放(百度语音API):https://blog.csdn.net/haoranhaoshi/article/details/87888430
Java 语音聊天机器人(百度语音API)(当前预置问答,可用图灵机器人框架扩展):
https://blog.csdn.net/haoranhaoshi/article/details/87888469 (依赖前三篇博客代码)
百度语音实战下载:https://download.csdn.net/download/haoranhaoshi/10976591
图灵机器人(Java文字版):https://blog.csdn.net/haoranhaoshi/article/details/87992548
图灵机器人(Java语音版):https://blog.csdn.net/haoranhaoshi/article/details/87992661

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

/**
 * 按住按钮或者按住Ctrl说话
 */
public class ChatWithRobot extends Application {
    private VoiceCompose voiceCompose = new VoiceCompose();
    private VoiceRecognition voiceRecognition = new VoiceRecognition();
    private VoiceRecorder voiceRecorder = new VoiceRecorder();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("按住按钮或者按住Ctrl说话");
        Button button = new Button("按住说话");
        FlowPane flowPane = new FlowPane(label, button);
        flowPane.setAlignment(Pos.CENTER);
        flowPane.setOrientation(Orientation.HORIZONTAL);
        flowPane.setPrefHeight(80);
        flowPane.setPrefWidth(200);
        flowPane.setVgap(20);

        button.setOnMousePressed(event -> voiceRecorder.captureAudio());

        button.setOnMouseReleased(event -> responseMaster());

        flowPane.setOnKeyPressed(event -> {
                    if (event.getCode() == KeyCode.CONTROL) {
                        voiceRecorder.captureAudio();
                    }
                }
        );

        flowPane.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.CONTROL) {
                responseMaster();
            }
        });

        primaryStage.setScene(new Scene(flowPane));
        primaryStage.show();
    }

    private void responseMaster(){
        voiceRecorder.closeCaptureAudio();

        if(voiceRecognition.recognizeVoice()){
            String resultText = voiceRecognition.getResultText();
            System.out.println("结果为:" + resultText);
            String text = "主人,我不知道怎么回答你!";
            switch (resultText){
                case "感觉如何?":
                    text = "我很好!";
                    break;
                case "开心吗?":
                    text = "我很开心!";
                    break;
            }

            if(!voiceCompose.getMP3ByText(text)){
                System.out.println("转换失败");
            }else{
                voiceCompose.playMP3();
            }
        }else{
            System.out.println("识别错误");
        }
    }
}

 

你可能感兴趣的:(Java,百度语音)