Java mp3转wav 微信小程序使用百度语音识别不兼容问题
编写时间:2019年6月30日 16:02:37
解决微信小程序中的录音格不符合百度语音识别的口味的问题
/**
* @param inputStream 微信上传的mp3InputSteam:voice.getInputStream()
* @return pcmBytes
* @throws Exception Exception
*/
public static byte[] mp3ToPcm(InputStream inputStream) throws Exception {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
AudioInputStream mp3 = AudioSystem.getAudioInputStream(bufferedInputStream);
AudioFormat mp3Format = mp3.getFormat();
AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, mp3Format.getSampleRate(), 16,
mp3Format.getChannels(), mp3Format.getChannels() * 2, mp3Format.getSampleRate(), false);
AudioInputStream pcm = AudioSystem.getAudioInputStream(pcmFormat, mp3);
byte[] pcmBytes = IOUtils.toByteArray(pcm);
bufferedInputStream.close();
mp3.close();
pcm.close();
return pcmBytes;
}
依赖的jar包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
如果报could not get audio input stream from input stream,那是可能是因为微信开发者工具的问题,使用真机调试就可以了
微信录音设置,因版本可能变更,具体设置参考官方文档:
const options = {
duration: 2000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 96000,
format: \'mp3\',
frameSize: 50
}
请发表评论