百度智能云之语音技术(自动播报语音)
生活随笔
收集整理的這篇文章主要介紹了
百度智能云之语音技术(自动播报语音)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、需求
商城平臺在收到客戶付款成功后自動語音播報“系統(tǒng)到賬1000元”。
二、解決思路
1、客戶支付成功,后臺生成訂單;
2、后臺推送一條消息到客戶端;
3、客戶端收到消息后調(diào)用第三方接口將文字轉(zhuǎn)成語音文件播放。
三、實踐(本案例使用百度語音技術(shù))
1、申請百度智能云賬號
2、進入語音技術(shù)模塊創(chuàng)建一個應(yīng)用(文檔地址)
3、換取token
// appKey = Va5yQRHl********LT0vuXV4 // appSecret = 0rDSjzQ20XUj5i********PQSzr5pVw2 https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHl********LT0vuXV4&client_secret=0rDSjzQ20XUj5i********PQSzr5pVw2
{
"access_token": "1.a6b7dbd428f731035f771b8d********.86400.1292922000-2346678-124328",
"expires_in": 2592000,
"refresh_token": "2.385d55f8615fdfd9edb7c4b********.604800.1293440400-2346678-124328",
"scope": "public audio_tts_post ...",
"session_key": "ANXxSNjwQDugf8615Onqeik********CdlLxn",
"session_secret": "248APxvxjCZ0VEC********aK4oZExMB",
}
3、訪問合成接口
http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok=1.a6b7dbd428f731035f771b8d****.86400.1292922000-2346678-124328&tex=%e7%99%be%e5%ba%a6%e4%bd%a0%e5%a5%bd&vol=9&per=0&spd=5&pit=5&aue=3 // 這是一個正常MP3的下載url // tex在實際開發(fā)過程中請urlencode2次
4、測試客戶端截圖
5、測試客戶端代碼(本案例使用uniapp)
1 <template>
2 <view>
3 <view class="uni-padding-wrap uni-common-mt">
4 <view class="button-sp-area">
5 <button type="default" @click="changeAudio()">切換</button>
6 <button type="primary" @click="play()">播放</button>
7 <button type="primary" @click="pause()">暫停</button>
8 <button type="warn" @click="stop()">停止</button>
9 </view>
10 <view>
11 <view class="uni-title">測試文字</view>
12 <view>
13 <input v-model="readText" class="uni-input" maxlength="10" placeholder="最大輸入長度為10" />
14 </view>
15 <view class="uni-title">語速</view>
16 <view>
17 <slider v-model="spd" min="0" max="15" show-value @change="spdSliderChange" />
18 </view>
19 <view class="uni-title">音調(diào)</view>
20 <view>
21 <slider v-model="pit" min="0" max="15" show-value @change="pitSliderChange" />
22 </view>
23 <view class="uni-title">音量</view>
24 <view>
25 <slider v-model="vol" min="0" max="15" show-value @change="volSliderChange" />
26 </view>
27
28 <view class="uni-title">發(fā)音人</view>
29 <view class="uni-list">
30 <view class="uni-list-cell">
31 <view class="uni-list-cell-left">
32 當前選擇
33 </view>
34 <view class="uni-list-cell-db">
35 <picker @change="bindPickerChange" :value="index" range-key="lable" :range="array">
36 <view class="uni-input">{{array[index].lable}}</view>
37 </picker>
38 </view>
39 </view>
40 </view>
41 <button type="primary" @click="loadBaiduAudio2()">測試百度語音合成</button>
42 </view>
43 </view>
44 </view>
45 </template>
46
47 <script>
48 export default {
49 data() {
50 return {
51 innerAudioContext: null,
52 readText: '支付寶到賬一千元',
53 access_token: '00.ae748a58ea3160facbdf707c923b5309.2592000.1574926792.282335-16656017',
54 spd: 5,
55 pit: 5,
56 vol: 5,
57 per: 0,
58 array: [{
59 value: 0,
60 lable: '普通女聲'
61 },
62 {
63 value: 1,
64 lable: '普通男生'
65 },
66 {
67 value: 3,
68 lable: '情感合成-度逍遙'
69 },
70 {
71 value: 4,
72 lable: '情感合成-度丫丫'
73 }
74 ],
75 index: 0
76 }
77 },
78 onLoad() {
79 const innerAudioContext = uni.createInnerAudioContext();
80 innerAudioContext.autoplay = true;
81 innerAudioContext.src = 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/audio/music.mp3';
82 innerAudioContext.pause();
83 innerAudioContext.onPlay(() => {
84 console.log('開始播放');
85 });
86 innerAudioContext.onError((res) => {
87 console.log(res.errMsg);
88 console.log(res.errCode);
89 });
90 this.innerAudioContext = innerAudioContext;
91 },
92 methods: {
93 changeAudio: function() {
94 this.innerAudioContext.src = 'http://m10.music.126.net/20191029152112/53749fba54e8903c0daa20d7c723f3d6/ymusic/374b/8502/4d09/798b85bbef3192e6de5675159073e941.mp3';
95 },
96 play: function() {
97 this.innerAudioContext.play();
98 },
99 pause: function() {
100 this.innerAudioContext.pause();
101 },
102 stop: function() {
103 this.innerAudioContext.stop();
104 },
105 // GET方式
106 loadBaiduAudio2: function() {
107 this.innerAudioContext.src = 'http://tsn.baidu.com/text2audio?tex=' + encodeURIComponent(encodeURIComponent(this.readText)) +
108 '&tok=' + this.access_token + '&cuid=test_001&ctp=1&lan=zh&spd=' +
109 this.spd + '&pit=' + this.pit + '&vol=' + this.vol + '&per=' + this.per
110 },
111 // POST方式
112 loadBaiduAudio: function() {
113 uni.request({
114 url: 'http://tsn.baidu.com/text2audio',
115 method: 'POST',
116 data: {
117 // 必填 合成的文本,使用UTF-8編碼。小于2048個中文字或者英文數(shù)字。(文本在百度服務(wù)器內(nèi)轉(zhuǎn)換為GBK后,長度必須小于4096字節(jié))
118 tex: encodeURIComponent(encodeURIComponent(this.readText)),
119 // 必填 開放平臺獲取到的開發(fā)者access_token
120 tok: this.access_token,
121 // 必填 用戶唯一標識,用來計算UV值。建議填寫能區(qū)分用戶的機器 MAC 地址或 IMEI 碼,長度為60字符以內(nèi)
122 cuid: 'test_001',
123 // 必填 客戶端類型選擇,web端填寫固定值1
124 ctp: 1,
125 // 必填 固定值zh。語言選擇,目前只有中英文混合模式,填寫固定值zh
126 lan: 'zh',
127 // 選填 語速,取值0-15,默認為5中語速
128 spd: 5,
129 // 選填 音調(diào),取值0-15,默認為5中語調(diào)
130 pit: 5,
131 // 選填 音量,取值0-15,默認為5中音量
132 vol: 5,
133 // 選填 發(fā)音人選擇, 0為普通女聲,1為普通男生,3為情感合成-度逍遙,4為情感合成-度丫丫,默認為普通女聲
134 per: 0,
135 // 選填 3為mp3格式(默認); 4為pcm-16k;5為pcm-8k;6為wav(內(nèi)容同pcm-16k); 注意aue=4或者6是語音識別要求的格式,但是音頻內(nèi)容不是語音識別要求的自然人發(fā)音,所以識別效果會受影響。
136 aue: 3
137 },
138 header: {},
139 success: (res) => {
140 // this.innerAudioContext.src = res.data;
141 }
142 });
143 },
144 bindPickerChange: function(e) {
145 console.log('picker發(fā)送選擇改變,攜帶值為', e.target.value)
146 this.index = e.target.value
147 this.per = this.array[e.target.value].value || 0;
148 },
149 spdSliderChange: function(e) {
150 console.log('value 發(fā)生變化:' + e.detail.value);
151 this.spd = e.detail.value;
152 },
153 pitSliderChange: function(e) {
154 console.log('value 發(fā)生變化:' + e.detail.value);
155 this.pit = e.detail.value;
156 },
157 volSliderChange: function(e) {
158 console.log('value 發(fā)生變化:' + e.detail.value);
159 this.vol = e.detail.value;
160 }
161 }
162 }
163 </script>
164
165 <style>
166 button {
167 margin-top: 30upx;
168 margin-bottom: 30upx;
169 }
170
171 .button-sp-area {
172 margin: 0 auto;
173 60%;
174 }
175
176 .mini-btn {
177 margin-right: 10upx;
178 }
179 </style>
總結(jié)
以上是生活随笔為你收集整理的百度智能云之语音技术(自动播报语音)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS调试Ajax
- 下一篇: Python—Mysql—Dbvisua