package com.hello.audio;

import com.alibaba.fastjson.JSONObject;
import com.hello.http.HttpUtil;
import com.hello.http.RespJsonObj;
import com.hello.util.B64Util;

import java.io.ByteArrayOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

public class ChatUtil {
    public static void chat() {
        RecordHelper recordHelper = RecordHelper.getInst();
        final ByteArrayOutputStream data = recordHelper.save(new ByteArrayOutputStream());

        JSONObject ret = HttpUtil.sendHttpPost(
                "http://localhost:8011/speech/walle",
                null, new HashMap<String, Object>() {{
                    put("size", data.size());
                    put("format", "wav");
                    put("audio", B64Util.encode(data.toByteArray()));
                    put("url", "1");
                }}, new RespJsonObj()
        );
        System.out.println(ret);

        String fileUrl = ret.getString("msg");
        try {
            Player.asyncPlay(new URL(fileUrl));
        } catch (MalformedURLException e) {
            System.err.println(e.getMessage());
        }
    }
}

@@ -922,24 +922,81 @@
 ntln(ret);%0A%0A
+        if (ret != null && ret.containsKey(%22msg%22)) %7B%0A    
         Stri
@@ -1026,24 +1026,28 @@
 ing(%22msg%22);%0A
+    
         try 
@@ -1060,16 +1060,20 @@
         
+    
 Player.a
@@ -1108,16 +1108,20 @@
         
+ 
+   
 %7D catch 
@@ -1156,24 +1156,28 @@
             
+  
+  
 System.err.p
@@ -1196,24 +1196,112 @@
 Message());%0A
+            %7D%0A        %7D else %7B%0A            // %E6%92%AD%E6%94%BE%E8%87%AA%E5%B7%B1%E7%9A%84%E5%A3%B0%E9%9F%B3%E5%90%A7%0A            recordHelper.play();%0A
         %7D%0A  

fix error when no response from server