java stie:baiducom_JAVA调用百度AI实现计算机语音交互

1. 主要用来增强交互体验

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.speech.AipSpeech;

import com.baidu.aip.speech.TtsResponse;

import com.baidu.aip.util.Util;

import javazoom.jl.player.Player;

public class SpeakController {

// 设置APPID/AK/SK

public static final String APP_ID = "";

public static final String API_KEY = "";

public static final String SECRET_KEY = "";

// 配置信息

public AipSpeech client;

public SpeakController() {

// 初始化一个AipSpeech

client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

}

public void speak(String content) {

new Thread(new Runnable() {

@Override

public void run() {

// 调用接口

// 设置可选参数

HashMap options = new HashMap();

options.put("spd", "7");

options.put("pit", "7");

options.put("per", "4");

TtsResponse res = client.synthesis(content, "zh", 1, options);

byte[] data = res.getData();

JSONObject res1 = res.getResult();

if (data != null) {

try {

Util.writeBytesToFileSystem(data, "speak.mp3");

// 音频播放

load();

} catch (Exception e) {

e.printStackTrace();

}

}

if (res1 != null) {

System.out.println(res1.toString(2));

}

}

}).start();

}

public void load() {

Player player;

BufferedInputStream buffer;

try {

buffer = new BufferedInputStream(new FileInputStream("speak.mp3"));

player = new Player(buffer);

player.play();

if (buffer != null) {

buffer.close();

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

声音是可以更换的,具体看百度AI说明文档

标签:JAVA,String,AI,options,buffer,import,new,交互,public

来源: https://www.cnblogs.com/xuyi007/p/11150743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值