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
        }
posted on 2019-06-30 16:06 凉云 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liangyun/p/11110190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值