Java实现智能对话机器人自动聊天+语音秒回
生活随笔
收集整理的這篇文章主要介紹了
Java实现智能对话机器人自动聊天+语音秒回
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
技術說明:
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實現(xiàn)文本轉語音
需要資源或代碼有疑問的可加我的技術討論q群1005611884
3. 使用Java Swing實現(xiàn)簡單的聊天界面
【完整代碼】
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("發(fā)送");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;}//實現(xiàn)文字轉語音 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));// 執(zhí)行朗讀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