淘先锋技术网

首页 1 2 3 4 5 6 7

首先得开发百度语音合成权限

文档地址:http://yuyin.baidu.com/docs/tts


public class Sample {

    private static final String serverURL = "http://tsn.baidu.com/text2audio?";
    private static String token = "";
    //put your own params here
    private static final String apiKey = "***";
    private static final String secretKey = "***";
    private static final String cuid = "74-D4-35-FB-AD-3F";//自己电脑的mac地址


    public static void main(String[] args) throws Exception {
        getToken();
        method1();
    }


    private static void getToken() throws Exception {
        String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + 
            "&client_id=" + apiKey + "&client_secret=" + secretKey;
        HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection();
        token = new JSONObject(printResponse(conn)).getString("access_token");
    }


    private static void method1() throws Exception {
     String content ="中国";
      Sample.readContentFromGet(serverURL+"tex="+content+"&cuid="+cuid+"&ctp=1&tok="+token+"&lan=zh");
    }
   


   


    private static String printResponse(HttpURLConnection conn) throws Exception {
        if (conn.getResponseCode() != 200) {
            // request error
            return "";
        }
        InputStream is = conn.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        System.out.println(new JSONObject(response.toString()).toString(4));
        return response.toString();

    }

//get请求

    public static void readContentFromGet(String url) throws IOException {
        // 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
    System.out.println(url);
        String getURL = url +URLEncoder.encode("fat man", "utf-8");
        URL getUrl = new URL(url);
        // 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
        // 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
        HttpURLConnection connection = (HttpURLConnection) getUrl
                .openConnection();
        // 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
        // 服务器
        connection.connect();
        // 取得输入流,并使用Reader读取
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        System.out.println("=============================");
        System.out.println("Contents of get request");
        System.out.println("=============================");
        String lines;
        while ((lines = reader.readLine()) != null) {
            System.out.println(lines);
        }
        reader.close();
        // 断开连接
        connection.disconnect();
        System.out.println("=============================");
        System.out.println("Contents of get request ends");
        System.out.println("=============================");
    }


}