1. 程式人生 > >微信小程式口令紅包-語音識別

微信小程式口令紅包-語音識別

開發微信小程式口令紅包埋坑:

1、小程序錄音

使用新版的RecorderManager進行錄音操作。
image.png
不要使用
image.png

新版的上傳錄音的格式為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>