当前位置:
首页 >
Java实现智能对话机器人自动聊天+语音秒回
发布时间:2023/12/20
64
豆豆
生活随笔
收集整理的这篇文章主要介绍了
Java实现智能对话机器人自动聊天+语音秒回
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
技术说明:
1. 调用青云课人工智能聊天API接口
| 请求地址 | http://api.qingyunke.com/api.php | ||
| 请求方式 | GET | 字符编码 | utf-8 |
| 请求示例 | http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好 | ||
2. 下载jacob.jar实现文本转语音
需要资源或代码有疑问的可加我的技术讨论q群1005611884
3. 使用Java Swing实现简单的聊天界面
【完整代码】
public class Demo {public static void main(String[] args){JFrame jf = new JFrame("深夜聊天室");jf.setSize(580, 600);jf.setLocation(150, 150);jf.add(new MyPanel());jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setResizable(false);jf.setVisible(true);} } public static class MyPanel extends JLayeredPane{ArrayList<String> allMsg = new ArrayList<>();JTextArea jTextRecode = new JTextArea();public MyPanel(){JTextArea jTextMsg = new JTextArea();jTextMsg.setBorder(new BevelBorder(1,Color.gray,Color.gray));jTextMsg.setLocation(20,400);jTextMsg.setSize(500, 100);this.add(jTextMsg);JButton btnSend = new JButton();btnSend.setText("发送");btnSend.setBounds(450,500,70,30);btnSend.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {String me =jTextMsg.getText();jTextMsg.setText("");allMsg.add(me);jTextRecode.setText(listTostr(allMsg));repaint();new Thread(){@Overridepublic void run() {URL url = null;try {url = new URL("http://api.qingyunke.com/api.php?key=free&appid=0&msg="+me);InputStream is = url.openStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String content = br.readLine();JSONObject obj = JSONObject.fromObject(content);content = obj.getString("content");allMsg.add("菲菲:"+content);jTextRecode.setText(listTostr(allMsg));repaint();speak(content);} catch (MalformedURLException ex) {ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}}.start();}});this.add(btnSend);jTextRecode.setBorder(new BevelBorder(1,Color.gray,Color.gray));jTextRecode.setLocation(20,20);jTextRecode.setFont(new Font("楷体",0,18));jTextRecode.setSize(500, 400);this.add(jTextRecode);}//集合转字符串 public static String listTostr(ArrayList list){String content = "";for(int i=0;i<list.size();i++){content=content+list.get(i)+"\n";}return content;}//实现文字转语音 public static void speak(String content){ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");Dispatch sapo = sap.getObject();try {// 音量 0-100sap.setProperty("Volume", new Variant(100));// 语音朗读速度 -10 到 +10sap.setProperty("Rate", new Variant(3));// 执行朗读System.out.println("菲菲:"+content);Dispatch.call(sapo, "Speak", new Variant(content));} catch (Exception e) {e.printStackTrace();} finally {sapo.safeRelease();sap.safeRelease();}}}需要资源或代码有疑问的可加我的技术讨论q群1005611884
总结
以上是生活随笔为你收集整理的Java实现智能对话机器人自动聊天+语音秒回的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C++ virtual 析构函数
- 下一篇: Java 将Word转为OFD