微信小程序:利用搜狗API实现听写单词小程序
wxml
<view>
<input bindconfirm="addword" confirm-type="AddWord" focus="{{focus}}" hold-keyboard="true" confirm-hold="true" type="text" bindinput="inputdata" placeholder="输入单词的汉语" value="{{value}}"/>
<button bindtap="addword">添加</button>
<button bindtap="detele">删除上一条</button>
<button bindtap="randomread">随机听写</button>
<button bindtap="againread">再次听写</button>
<view style="display: flex;width: 100%;height: auto;flex-wrap: wrap;">
<view wx:for="{{wordlist}}" style="margin: 40rpx;">{{item}},</view>
</view>
</view>
js
const myaudio = wx.createInnerAudioContext()
Page({
/**
* 页面的初始数据
*/
data: {
wordlist:[],
wordlist1:[],
value:"",
random:Number,
focus:true
},
confirmTap(e){
console.log(e);
},
inputdata(e){
console.log(e);
this.setData({
value:e.detail.value
})
},
addword(){
var wordlist = this.data.wordlist
wordlist.push(this.data.value)
this.setData({
wordlist:wordlist,
value:"",
focus:true
})
wx.setStorageSync('wordlist', wordlist)
},
// 删除
detele(){
let wordlist = this.data.wordlist;
wordlist.length = wordlist.length-1
this.setData({
wordlist:wordlist,
})
wx.setStorageSync('wordlist', wordlist)
},
againread(){
this.randomread()
},
randomread(){
wx.showToast({
title: '开始听写',
})
var timer;
let that = this;
let wordlist = that.data.wordlist;
timer = setInterval(function a(params) {
for (let i = 0; i < wordlist.length; i++) {
var num=Math.floor(Math.random()* wordlist.length+0);
myaudio.src = `https://fanyi.sogou.com/reventondc/synthesis?text=${wordlist[num]}&speed=1&lang=zh-CHS&from=translateweb&speaker=6`
console.log(wordlist[num]);
myaudio.play()
wordlist.splice(num,1)
console.log(wordlist);
return;
}
if (wordlist.length == 0) {
clearInterval(timer);
wx.showToast({
title: '听写完毕!',
icon:"success"
})
}
},5000)
that.setData({
wordlist:wx.getStorageSync('wordlist')
})
},
onLoad(){
var that = this
if (wx.getStorageSync('wordlist')) {
that.setData({
wordlist: wx.getStorageSync('wordlist')
})
}else{
}
}
})
效果图:
接口文档:
搜狗的开放转换接口
“https://fanyi.sogou.com/reventondc/synthesis?text=(朗读的文字)A&speed=1&lang=zh-CHS&from=translateweb&speaker=6”
text :要转换的文本
speed :语速 1~?(我测试到15都还可以) 越大,语速越慢
lan 语言类型
lan=en 英文
lan = zh-CHS 中文
speaker 语音类型 1-6的数字
其他接口文档
https://blog.csdn.net/qq_42460461/article/details/115378875