导入Jar包
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.11.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
编写工具类
public class BaiduUtil {
// 设置APPID/AK/SK,通过百度自己申请
public static final String APP_ID = "1111111111";
public static final String API_KEY = "11111111111111";
public static final String SECRET_KEY = "11111111111111";
/**
*
* @param sourcePath 转化前mp3文件地址
* @param targetPath 转化后文件地址
* @return
*/
public static Map<String, String> BaiduAPI(String sourcePath, String targetPath){
byteToWav(getBytes(sourcePath),targetPath);
return getAipSpeech(str + targetPath);
}
/**
* 转化
* @param targetPath
* @return
*/
public static Map<String, String> getAipSpeech(String targetPath) {
// 初始化一个AipSpeech
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 调用接口
JSONObject res = client.asr(targetPath, "wav", 16000, null);
Map<String, String> result = new HashMap<String, String>();
if("success.".equals(res.get("err_msg"))){
Iterator<String> iterator = res.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = iterator.next();
if ("result".equals(key)) {
Object re = res.get(key);
if ("JSONArray".equals(re.getClass().getSimpleName())) {
JSONArray s = (JSONArray)re;
value = s.getString(0);
}
}else{
value = res.get(key) + "";
}
result.put(key, value);
}
}else{
result.put("result","语音无法识别,转化失败");
}
System.out.println(result);
return result;
}
/**
* 将mp3文件转化为wav
* @param sourceBytes 二进制mp3文件,通过调取getBytes()方法
* @param targetPath 生成文件的地址
* @return
*/
public static boolean byteToWav(byte[] sourceBytes, String targetPath) {
if (sourceBytes == null || sourceBytes.length == 0) {
System.out.println("Illegal Argument passed to this method");
return false;
}
try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {
AudioFormat sourceFormat = sourceAIS.getFormat();
// 设置MP3的语音格式,并设置16bit
AudioFormat mp3tFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
// 设置百度语音识别的音频格式
AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, false);
try (
// 先通过MP3转一次,使音频流能的格式完整
final AudioInputStream mp3AIS = AudioSystem.getAudioInputStream(mp3tFormat, sourceAIS);
// 转成百度需要的流
final AudioInputStream pcmAIS = AudioSystem.getAudioInputStream(pcmFormat, mp3AIS)) {
// 根据路径生成wav文件
AudioSystem.write(pcmAIS, AudioFileFormat.Type.WAVE, new File(targetPath));
}
return true;
} catch (IOException e) {
System.out.println("文件转换异常:" + e.getMessage());
return false;
} catch (UnsupportedAudioFileException e) {
System.out.println("文件转换异常:" + e.getMessage());
return false;
}
}
/**
* 将文件转成字节流
* @param filePath
* @return
*/
private static byte[] getBytes(String filePath) {
System.out.println("filePath---->"+filePath);
byte[] buffer = null;
try {
/*File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);*/
URL url = new URL(filePath);
InputStream fis = url.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
}
测试
public class Test00 {
public static void main(String[] args) {
Map<String, String> result = new HashMap<>();
result = BaiduUtil.BaiduAPI("D:\\123.mp3", "D:\\file");
System.out.println(result);
}
}