Write Time: June 30, 2019 16:02:37
Micro-channel recording format to solve the problem of small programs do not meet the tastes of Baidu voice recognition
/**
* @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;
}
Dependent jar package
<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>
If the message could not get audio input stream from input stream, it may be because the problem is micro-channel developer tools, the use of real machine debugging on it
Micro-channel recording settings, depending on the version may change specific settings refer to the official document:
const options = {
duration: 2000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 96000,
format: 'mp3',
frameSize: 50
}