惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - 技术宅home

kali捕获wifi握手包 centos7安装mysql8 【继续教育】选准用好“三支一扶”人员,助力乡村振兴战略 【继续教育】深入学***在长三角一体化发展座谈会的重要讲话精神,推动长三角区域一体化和高质量发展 【继续教育】全球战“疫”斗争中的中国优势、 中国力量、中国担当、中国精神 【继续教育】民法典总则编解读 【继续教育】坚持创新在我国现代化建设全局中的核心地位,把科技自立自强作为国家发展的战略支撑 【继续教育】加快构建完整内需体系,形成国内国际双循环相互促进新格局 redis key过期提醒,实现订单自动取消 查看当前服务器中最占内存的程序 maven多环境IDEA启动报Do not use @ for indentation错误解决方法 StringRedisTemplate获取redis信息 MySQL规范 微信测试公众号手动发送模板消息 spring-boot-starter-parent 与 spring-boot-dependencies Docker 容器知识点 Docker 常用命令 Docker 镜像知识点 Centos离线下载Aria2 AriaNG bt-tracker自动更新
百度智能云之语音技术(自动播报语音)
技术宅home · 2019-10-29 · via 博客园 - 技术宅home

一、需求

商城平台在收到客户付款成功后自动语音播报“系统到账1000元”。

二、解决思路

1、客户支付成功,后台生成订单;

2、后台推送一条消息到客户端;

3、客户端收到消息后调用第三方接口将文字转成语音文件播放。

三、实践(本案例使用百度语音技术)

1、申请百度智能云账号

2、进入语音技术模块创建一个应用(文档地址

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在实际开发过程中请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">音调</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">发音人</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个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于4096字节)
118                         tex: encodeURIComponent(encodeURIComponent(this.readText)),
119                         // 必填    开放平台获取到的开发者access_token
120                         tok: this.access_token,
121                         // 必填    用户唯一标识,用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内
122                         cuid: 'test_001',
123                         // 必填    客户端类型选择,web端填写固定值1
124                         ctp: 1,
125                         // 必填    固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh
126                         lan: 'zh',
127                         // 选填    语速,取值0-15,默认为5中语速
128                         spd: 5,
129                         // 选填    音调,取值0-15,默认为5中语调
130                         pit: 5,
131                         // 选填    音量,取值0-15,默认为5中音量
132                         vol: 5,
133                         // 选填    发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
134                         per: 0,
135                         // 选填    3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
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发送选择改变,携带值为', 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 发生变化:' + e.detail.value);
151                 this.spd = e.detail.value;
152             },
153             pitSliderChange: function(e) {
154                 console.log('value 发生变化:' + e.detail.value);
155                 this.pit = e.detail.value;
156             },
157             volSliderChange: function(e) {
158                 console.log('value 发生变化:' + 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         width: 60%;
174     }
175 
176     .mini-btn {
177         margin-right: 10upx;
178     }
179 </style>