This topic describes how to use the WeChat mini program software development kit (SDK) for Alibaba Cloud Voice Service. It covers how to install the SDK and provides code samples.
Prerequisites
Before you use the SDK, read the API reference. For more information, see API reference.
Download and install
Download and install the SDK.
You can download the SDK code from Github or download the alibabacloud-nls-wx-sdk-master.zip file directly.
Import the SDK.
Copy the downloaded code to a folder in your project. Then, import the SDK using `require` and specifying the folder path.
Get a token
getToken
This method obtains a token and caches it using the AKID (AccessKey ID) and AKKEY (AccessKey Secret) as keys. If the cached token expires, the method automatically refreshes the token. For more information about the caching mechanism, see the Data Caching topic in the WeChat Mini Program documentation.
Parameters: None.
Return value: A string that represents the token.
getTokenInner
This method directly retrieves a token without using the caching mechanism. This method is suitable for custom caching.
Parameters: None.
Return value: A string that represents the token.
The server denies frequent calls to this API.
Key interfaces and parameters
To implement speech synthesis, use the SpeechSynthesizer class. You can write the code by following these steps. Steps 2 and 3 are interchangeable.
Create a
SpeechSynthesizerinstance and provide the speech synthesis endpoint and authentication information.Set speech synthesis properties, such as the voice, sample rate, and audio format. You can create a properties object or modify the default properties object that is returned by the
defaultStartParamsmethod of theSpeechSynthesizerinstance.Implement the
oncallback function of theSpeechSynthesizerinstance. This function uses the observer pattern. The server notifies the client by invoking theoncallback function when a connection is established, speech is synthesized, or an exception occurs.Call the
startfunction of theSpeechSynthesizerinstance to begin speech synthesis.
Code sample
The following code sample is for reference only. The code uses the built-in recording feature of WeChat mini programs. When you use the code, you must consider the limitations of WeChat mini programs, the frontend page design, and your specific business requirements.
// pages/tts/tts.js
const app = getApp()
const AKID = "Your AKID"
const AKKEY = "Your AKKEY"
const SpeechSynthesizer = require("../../utils/tts")
const formatTime = require("../../utils/util").formatTime
const sleep = require("../../utils/util").sleep
const getToken = require("../../utils/token").getToken
const fs = wx.getFileSystemManager()
Page({
/**
* Initial data of the page
*/
data: {
ttsStart: false,
ttsText: ""
},
/**
* Lifecycle function--Called when the page is loaded
*/
onLoad: async function (options) {
try {
this.data.token = await getToken(AKID, AKKEY)
} catch (e) {
console.log("error on get token:", JSON.stringify(e))
return
}
let tts = new SpeechSynthesizer({
url: app.globalData.URL,
appkey:app.globalData.APPKEY,
token:this.data.token
})
tts.on("meta", (msg)=>{
console.log("Client recv metainfo:", msg)
})
tts.on("data", (msg)=>{
console.log(`recv size: ${msg.byteLength}`)
//console.log(dumpFile.write(msg, "binary"))
if (this.data.saveFile) {
try {
fs.appendFileSync(
this.data.saveFile,
msg,
"binary"
)
console.log(`append ${msg.byteLength}`)
} catch (e) {
console.error(e)
}
} else {
console.log("save file empty")
}
})
tts.on("completed", async (msg)=>{
console.log("Client recv completed:", msg)
await sleep(500)
fs.close({
fd : this.data.saveFd,
success: (res)=> {
let ctx = wx.createInnerAudioContext()
ctx.autoplay = true
ctx.src = this.data.saveFile
ctx.onPlay(() => {
console.log('start playing..')
})
ctx.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
fs.unlink({
filePath: this.data.saveFile,
success: (res)=>{
console.log(`remove ${this.data.saveFile} done`)
this.data.saveFile = null
this.data.saveFd = null
},
failed: (res)=>{
console.log("remove failed:" + res.errMsg)
}
})
})
ctx.onEnded((res)=>{
console.log("play done...")
fs.unlink({
filePath: this.data.saveFile,
success: (res)=>{
console.log(`remove ${this.data.saveFile} done`)
this.data.saveFile = null
this.data.saveFd = null
},
failed: (res)=>{
console.log("remove failed:" + res.errMsg)
}
})
})
},
fail : (res)=>{
console.log("saved file error:" + res.errMsg)
}
})
})
tts.on("closed", () => {
console.log("Client recv closed")
})
tts.on("failed", (msg)=>{
console.log("Client recv failed:", msg)
})
this.data.tts = tts
},
/**
* Lifecycle function--Called when the page is first rendered
*/
onReady: function () {
},
/**
* Lifecycle function--Called when the page is displayed
*/
onShow: function () {
},
/**
* Lifecycle function--Called when the page is hidden
*/
onHide: function () {
},
/**
* Lifecycle function--Called when the page is uninstalled
*/
onUnload: function () {
},
/**
* Page-related event handler function--Listens for user pull-down actions
*/
onPullDownRefresh: function () {
},
/**
* Handler function for the page pull-up and bottom-reaching event
*/
onReachBottom: function () {
},
/**
* Called when the user clicks the share button in the upper-right corner
*/
onShareAppMessage: function () {
},
textInput: function(e) {
this.setData({
ttsText:e.detail.value
})
},
onTtsStart: function() {
if (!this.data.ttsText || !this.data.tts) {
console.log("text empty")
wx.showToast({
title: "Text is empty",
icon: "error",
duration: 1000,
mask: true
})
return
}
if (this.data.ttsStart) {
wx.showToast({
title: "Synthesizing. Please wait.",
icon: "error",
duration: 1000,
mask: true
})
return
} else {
this.data.ttsStart = true
}
console.log("try to synthesize:" + this.data.ttsText)
let save = formatTime(new Date()) + ".wav"
let savePath = wx.env.USER_DATA_PATH + "/" + save
console.log(`save to ${savePath}`)
fs.open({
filePath:savePath,
flag : "a+",
success: async (res)=> {
console.log(`open ${savePath} done`)
this.data.saveFd = res.fd
this.data.saveFile = savePath
let param = this.data.tts.defaultStartParams()
param.text = this.data.ttsText
param.voice = "aixia"
try {
await this.data.tts.start(param)
console.log("tts done")
this.data.ttsStart = false
} catch(e) {
console.log("tts start error:" + e)
}
},
fail: (res)=> {
console.log(`open ${savePath} failed: ${res.errMsg}`)
}
})
}
})