基于SpringBoot和Vue的動(dòng)態(tài)語音播放實(shí)現(xiàn)
SpringBoot+Vue實(shí)現(xiàn)動(dòng)態(tài)語音播放。實(shí)現(xiàn)效果:
- 頁面點(diǎn)擊播報(bào)語音按鈕,調(diào)用后臺接口獲取二進(jìn)制byte[]
- 前端得到返回?cái)?shù)據(jù),調(diào)用瀏覽器控件AudioContext解碼音頻流,實(shí)現(xiàn)播放。
一、后臺接口返回byte[]
1、在controller中添加接口,返回byte[]
- 設(shè)置 produces = “application/octet-stream”
- 設(shè)置 返回類型 ResponseEntity<byte[]>
@PostMapping(value = "/v/voice", produces = "application/octet-stream") public ResponseEntity<byte[]> voice(@RequestBody JSONObject param, HttpServletResponse response) throws IOException { String text = param.getString("text"); // 以下代碼調(diào)用阿里云接口,把文字轉(zhuǎn)語音 byte[] voice = SpeechRestfulUtil.text2voice(text); // 返回音頻byte[] return ResponseEntity.ok().body(voice); }
本例是調(diào)用阿里云tts接口,把文字轉(zhuǎn)語音
2、在configureMessageConverters中添加解析器
ByteArrayHttpMessageConverter
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(objectMapper()); converters.add(jackson2HttpMessageConverter); converters.add(new ByteArrayHttpMessageConverter()); }
二、Vue前端調(diào)用接口播放語音
使用axios調(diào)用后端接口,設(shè)置 responseType=blob
1)得到瀏覽器播放控件 audioContext
2)使用FileReader讀取得到的byte[]
3)FileReader綁定load事件,讀取byte[]完成后播放語音
function doVoice(){ axios({ method:'post', url:req.voice, responseType:'blob', data:{text:data.info} // 需要播放的文本 }).then(function (response) { console.log(response); if(response.status===200){ // 1)得到瀏覽器播放控件 audioContext let audioContext = new (window.AudioContext || window.webkitAudioContext)(); let reader = new FileReader(); reader.onload = function(evt) { if (evt.target.readyState === FileReader.DONE) { // 3)FileReader綁定load事件,讀取byte[]完成后播放語音 audioContext.decodeAudioData(evt.target.result, function(buffer) { // 解碼成pcm流 let audioBufferSouceNode = audioContext.createBufferSource(); audioBufferSouceNode.buffer = buffer; audioBufferSouceNode.connect(audioContext.destination); audioBufferSouceNode.start(0); }, function(e) { console.log(e); }); } }; // 2)使用FileReader讀取得到的byte[] reader.readAsArrayBuffer(response.data); } }) .catch(function (error) { // handle error console.log(error); }) .finally(function () { // always executed }); }
到此這篇關(guān)于基于SpringBoot和Vue的動(dòng)態(tài)語音播放實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot動(dòng)態(tài)語音播放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線性結(jié)構(gòu)中棧、隊(duì)列和串的基本概念和特點(diǎn)詳解
前幾天小編給大家介紹了Java線性結(jié)構(gòu)中的鏈表,除了鏈表這種結(jié)構(gòu)之外,實(shí)際上還有棧、隊(duì)列、串等結(jié)構(gòu),那么這些結(jié)構(gòu)又有哪些特點(diǎn)呢,本文就給大家詳細(xì)的介紹一下,感興趣的小伙伴跟著小編一起來看看吧2023-07-07Java防止頻繁請求、重復(fù)提交的操作代碼(后端防抖操作)
在客戶端網(wǎng)絡(luò)慢或者服務(wù)器響應(yīng)慢時(shí),用戶有時(shí)是會(huì)頻繁刷新頁面或重復(fù)提交表單的,這樣是會(huì)給服務(wù)器造成不小的負(fù)擔(dān)的,同時(shí)在添加數(shù)據(jù)時(shí)有可能造成不必要的麻煩,今天通過本文給大家介紹下Java防止頻繁請求、重復(fù)提交的操作代碼,一起看看吧2022-04-04spring boot 下對JSON返回值去除null和空字段操作
這篇文章主要介紹了spring boot 下對JSON返回值去除null和空字段操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10關(guān)于Java實(shí)現(xiàn)HttpServer模擬前端接口調(diào)用
這篇文章主要介紹了關(guān)于Java實(shí)現(xiàn)Http?Server模擬前端接口調(diào)用,Http?協(xié)議是建立在?TCP?協(xié)議之上的協(xié)議,所以能用?TCP?來自己模擬一個(gè)簡單的?Http?Server?當(dāng)然是可以的,需要的朋友可以參考下2023-04-04