开发微信小程序口令红包埋坑:
1、小程序录音
使用新版的RecorderManager进行录音操作。
不要使用
新版的上传录音的格式为mp3,旧版的录音格式为silk,后面做语音识别的时候非常难转换。
2、百度语音识别
百度语音识别支持的格式为pcm或者wav,所以需要对其进行转换。转换工具类代码如下
package com.gizhi.guns.core.util;
import javax.sound.sampled.*;
import java.io.*;
public class MP3ToWav {
/**
* mp3的字节数组生成wav文件
* @param sourceBytes
* @param targetPath
*/
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) {
e.printStackTrace();
System.out.println("文件转换异常:" + e.getMessage());
return false;
}
}
/**
* 将文件转成字节流
* @param filePath
* @return
*/
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
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 static void main(String args[]) {
String filePath = "d:/tmp/test.mp3";
String targetPath = "d:/tmp/test1.wav";
byteToWav(getBytes(filePath), targetPath);
}
}
注意点:必须引入支持mp3格式的依赖,否则在转换时会报出异常
<!--mp3插件-->
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>