uni-app使用百度語音識別轉文字

1. 準備

  • 註冊登錄百度AI開放平臺(http://ai.baidu.com/
  • 找到語音技術 - 應用列表,創建應用,填寫相應信息,語音包名選擇不需要在這裏插入圖片描述
  • 創建完成後,可以看到AppID、API Key、Secret Key

2. uni-app端開發

<template>
	<view>
		<button @tap="startRecord">開始錄音</button>
		<button @tap="endRecord">停止錄音</button>
		<button @tap="playVoice">播放錄音</button>
	</view>
</template>
<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	var _this;
	
	export default {
		data() {
			return {
			}
		},
		onLoad() {
			_this = this;
			recorderManager.onStop(function(res) {
				console.log('recorder stop' + JSON.stringify(res));
				// 使用uni.uploadFile上傳到服務器上,此時是mp3格式
			});
		},
		methods: {
			startRecord() {
				console.log('開始錄音');
				recorderManager.start({
					sampleRate: 16000 // 必須設置是後臺設置的參數,不然百度語音識別不了
				});
			},
			endRecord() {
				console.log('錄音結束');
				_this = this;
				recorderManager.stop();
			},
			playVoice() {
				console.log('播放錄音');

				if (this.voicePath) {
					innerAudioContext.src = this.voicePath;
					innerAudioContext.play();
				}
			}
		}
	}
</script>

<style>
</style>

3. java端開發

maven導入百度sdk,先將mp3轉換爲pcm,然後把pcm轉換成二進制調用api

<dependency>
    <groupId>com.baidu.aip</groupId>
    <artifactId>java-sdk</artifactId>
    <version>4.12.0</version>
</dependency>
<!--mp3轉pcm-->
<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>mp3spi</artifactId>
    <version>1.9.5.4</version>
</dependency>
public class SpeechRecognitionUtil {

    //設置APPID/AK/SK
    public static final String APP_ID = "";
    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    // 獲取AipSpeech對象,建議單例使用
    public static AipSpeech getClient() {
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        // 可選:設置網絡連接參數
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    // 語音識別(來自文件)
    public static JSONObject basicByFile(String path, String fileType) {
        AipSpeech client = getClient();
        return client.asr(path, fileType, 16000, null);
    }

    // 語音識別(來自二進制)
    public static JSONObject basicByByte(String filePath) throws IOException {
        AipSpeech client = getClient();
        byte[] data = Util.readFileByBytes(filePath);
        return client.asr(data, "pcm", 16000, null);
    }

    public static void main(String[] args) throws IOException {
        String mp3 = "D:/aa.mp3";
        String pcm = "D:/aa.pcm";
        MultiMediaUtil.MP3ConvertPcm(mp3,pcm);
        JSONObject jsonObject = basicByByte(pcm);
        System.out.println(jsonObject);
    }
}
public class MultiMediaUtil {

    // mp3轉pcm
    public static boolean MP3ConvertPcm(String mp3filepath, String pcmfilepath){
        try {
            //獲取文件的音頻流,pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //將音頻轉化爲  pcm的格式保存下來
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 獲得pcm文件的音頻流
     * @param mp3filepath
     * @return
     */
    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            AudioInputStream in = null;
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章