当前位置:网站首页>Mini Program-Voice broadcast function
Mini Program-Voice broadcast function
2022-08-10 14:53:00 【Yin qiu yi】
功能需求
Implemented by passing in“Voice copy is automatically broadcast”,如:“Test broadcast”,Voice broadcast automatically“Test broadcast”
方案
使用wx.playVoice播放语音文件
优点:使用简单,Play recorded audio files directly
缺点:Only recorded voice files can be played
Third-party voice services + wx.createInnerAudioContext
优点:功能强大,可定制,Can meet complex scenarios;
缺点:Server connection is required,付费
小程序插件 + wx.createInnerAudioContext
优点:简单快捷,适用于简单场景;
缺点:功能单一,无法定制,Uppercase English is not recognized,Some lowercase English pronunciations are inaccurate
Third-party voice platforms
语音平台
服务端
对接第三方平台,Give the converted voice data to the applet.
小程序
1.添加请求
wx.request({
url: 'https://xxxx.com',
data: { data: "The speech data to be synthesized"},
method: "get",
header: {
'content-type': 'application/json' // 默认值
},
dataType: "json",
success: function (res) {
const innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.src = res.filename;
innerAudioContext.play();
}
})
微信插件
添加 微信同声传译
插件:Configured on the WeChat public platform,找到设置–第三方设置–插件管理–点击添加插件搜索 微信同声传译
,点击添加引入插件:在项目根目录app.json文件中配置
"plugins": {
// 引入插件
"WechatSI": { // 自定义的名字
"version": "0.3.5", // 引入插件的版本号
"provider": "wx069ba97219f66d99" // 引入插件的appID
}
}
使用
const plugin = requirePlugin('WechatSI');
onReady() {
this.innerAudioContext = wx.createInnerAudioContext();
const that = this;
plugin.textToSpeech({
// 调用插件的方法
lang: 'zh_CN',
content: 'Test the voice broadcast',
success: function (res) {
that.playAudio(res.filename);
}
});
},
playAudio(e) {
this.innerAudioContext.src = e;
this.innerAudioContext.play();
},
封装优化
const plugin = requirePlugin('WechatSI');
import { trim, isLetter } from '@/shared/index';
class PluginService {
innerAudioContext;
audioBroadcast(content: string): Promise<any> {
return new Promise((resolve, reject) => {
this.innerAudioContext = wx.createInnerAudioContext();
plugin.textToSpeech({
// 调用插件的方法
lang: 'zh_CN',
content,
success: res => {
this.playAudio(res.filename, resolve, reject);
},
fail: reject
});
});
}
playAudio(e, resolve, reject) {
this.innerAudioContext.src = e;
this.innerAudioContext.play();
this.innerAudioContext.onStop(() => {
console.log('[ onStop ] >');
resolve({
type: 'stop'
});
});
this.innerAudioContext.onEnded(() => {
//播放结束,销毁该实例
this.innerAudioContext.destroy();
console.log('[ onEnded ] >');
resolve({
type: 'end'
});
});
this.innerAudioContext.onError(error => {
console.log('[ onError ] >', error);
this.innerAudioContext.destroy();
reject(error);
});
}
stopAudio() {
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
}
}
export default new PluginService();
在shared/indexMedium utility function:
// 去掉字符串中所有空格(包括中间空格,需要设置第2个参数为:true)
export function trim(str: string, isGlobal = true) {
if (!str) {
return str;
}
const result = str.replace(/(^\s+)|(\s+$)/g, '');
return isGlobal ? result.replace(/\s/g, '') : result;
}
// 判断是否为字母
export function isLetter(str) {
const reg = /^[A-Za-z]+$/;
return reg.test(str);
}
// Alphabet to Chinese
export function translateAlphabet(letters) {
if (!letters || !(letters.length > 0)) {
return letters;
}
let tLetters = '';
for (const key of letters) {
tLetters += alphabetComparison(key);
}
return trim(tLetters);
}
// Alphabet Chinese reading
export function alphabetComparison(letter) {
if (!letter || !isLetter(letter)) {
return letter;
}
const letterCase = letter.toUpperCase();
const letterMap = {
A: '诶',
B: 'b',
C: '西',
D: '帝',
E: '伊',
F: 'f',
G: 'g',
H: 'h',
I: 'i',
J: 'j',
K: 'k',
L: 'l',
M: 'm',
N: 'n',
O: '欧',
P: 'p',
Q: 'q',
R: 'r',
S: '爱死',
T: 't',
U: '优',
V: 'v',
W: 'w',
X: 'x',
Y: '歪',
Z: 'z'
};
return letterMap[letterCase];
}
在页面中使用
import { pluginService, translateAlphabet } from '@util';
import { formatCabinetNo, getCacheCabinet } from '@/shared/index';
Page({
...
onLoad(e) {
// Get order result type
this.autoPlay();
},
onUnload() {
pluginService.stopAudio();
},
// Polling to play multiple times
autoPlay(timerCount = 0, maxCount = 100) {
const { address, lockerName } = this.data;
console.log('timerCount:', timerCount);
if (timerCount > maxCount) {
return;
}
const tAddress = translateAlphabet(address);
console.log('[ translateAlphabet ] >', tAddress);
// 'Check the cabinet door number before storing,Don't save the wrong door'
const message = `Your cabinet door number is${tAddress}${lockerName},Please check the cabinet door number for storage,Don't save the wrong door`;
pluginService
.audioBroadcast(message)
.then(res => {
if (res.type !== 'stop') {
this.autoPlay(++timerCount);
}
console.log('audioBroadcast.complete', res);
})
.catch(err => {
console.log('[ audioBroadcast.err ] >', err);
this.autoPlay(timerCount);
});
}
});
注意:
Voice input quota:每个小程序250条/分钟,3w条/天. Text translation quota:每个小程序500次/分钟,10w次/天. Speech synthesis quota:每个小程序100次/分钟,2w次/天.
问题归档
在小程序onHide,onUnload函数里调用 stop
方法无效;需要同时调用destroy
方法
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
After testing, it was found that uppercase English letters in Chinese could not be recognized,Some lowercase English letters are pronounced incorrectly
参考
The mobile terminal quickly accesses the voice broadcast WeChat Simultaneous Interpretation applet plugin —— 机器翻译、智能语音
关注我
利用空闲时间免费兼职(无门槛,长期有效),请联系我(PS:关注公众号后,点击“联系我”获取联系方式)~
边栏推荐
- 串口服务器调试助手使用教程,串口调试助手使用教程【操作方式】
- 1004(树状数组+离线操作+离散化)
- 高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
- 宝塔面板开放Redis给指定外网机器
- 物资采购小程序开发制作功能介绍
- 【MindSpore易点通机器人-02】设计与技术选型
- How is the monthly salary table stored in the database?Ask for a design idea
- 关于已拦截跨源请求CORS 头缺少 ‘Access-Control-Allow-Origin‘问题解决
- SWIG教程《四》-go语言的封装
- Appium进行APP自动化测试
猜你喜欢
Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
MySQL 原理与优化:Update 优化
奢侈品鉴定机构小程序开发制作功能介绍
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
中学数学建模书籍及相关的视频等(2022.08.09)
微信小程序,自定义输入框与导航胶囊对其
波士顿房价预测
Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth
重要通知 | “移动云杯”算力网络应用创新大赛初赛延期!!
随机推荐
List集合
池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
小程序-语音播报功能
Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
EVE模拟器的使用-带图超详细(学网络用)「建议收藏」
使用mysq语句操作数据库
epoll学习:思考一种高性能的服务器处理框架
awk的简单使用
Go Context基本使用
Epoll learn to think: a high performance server processing framework
"Thesis Reading" PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
格式化输出当前时间
Steam教育在新时代中综合学习论
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
Understanding_Data_Types_in_Go
2022-08-10日报: Swin Transformer作者曹越加入智源,开展视觉基础模型研究
leetcode 739. Daily Temperatures 每日温度(中等)
注意力模型---Attention Model
d为何用模板参数
1004 (tree array + offline operation + discretization)