<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>
maven导入百度sdk,先将mp3转换为pcm,然后把pcm转换成二进制调用api
<dependency>
<groupId>com.baidu.aipgroupId>
<artifactId>java-sdkartifactId>
<version>4.12.0version>
dependency>
<dependency>
<groupId>com.googlecode.soundlibsgroupId>
<artifactId>mp3spiartifactId>
<version>1.9.5.4version>
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;
}
}