首页 > 编程知识 正文

uniapp 文字转语音,uniapp 语音识别

时间:2023-05-03 20:53:12 阅读:285352 作者:2515

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) { 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; }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。