重庆博达建设集团股份有限公司网站,徐州建设企业网站,桂林相关网站,托管公司是怎么托管的功能介绍#xff1a;
请求语音合成服务#xff0c;通过上传语音合成文本#xff0c;返回音频数据#xff0c;并保存到本地。这里要说明一下#xff0c;由于HttpResponse接口给问题#xff0c;服务的响应类型必须是application/octet-stream#xff0c;才能正确获取音频…功能介绍
请求语音合成服务通过上传语音合成文本返回音频数据并保存到本地。这里要说明一下由于HttpResponse接口给问题服务的响应类型必须是application/octet-stream才能正确获取音频数据并保存接口文档HttpResponse。
语音合成服务可以参考轻松快速搭建一个本地的语音合成服务
使用环境
API 9DevEco Studio 4.0 ReleaseWindows 11Stage模型ArkTS语言
所需权限
ohos.permission.INTERNET只保存在应用文件夹不涉及额外目录不需要申请读写权限
注意 只适合小于5M数据。
关键代码片段如下 async download() {if (this.text )returnpromptAction.showToast({ message: 合成文本 this.text })let httpRequest http.createHttp();let context getContext(this) as common.UIAbilityContext;const filesDir context.filesDir;let promise httpRequest.request(this.ttsUrl, {method: http.RequestMethod.POST,header: { Content-Type: application/json; charsetutf-8 },extraData: { text: this.text }})promise.then((data) {const timestamp Date.now();const savePath filesDir /${timestamp}.wavconsole.info(保存路径 savePath)let file fs.openSync(savePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);// ts-ignorefs.write(file.fd, data.result).then((writeLen) {fs.closeSync(file);console.info(已成功保存文件文件大小为 writeLen);}).catch((err) {console.error(保存文件出错错误信息 err.message , 错误代码 err.code);});}).catch((err) {console.error(错误信息 JSON.stringify(err))})
}完整代码
import promptAction from ohos.promptAction;
import http from ohos.net.http;
import common from ohos.app.ability.common;
import fs from ohos.file.fs;Entry
Component
struct Index {State text: string State ttsUrl: string http://xxxx.xxxxbuild() {Row() {TextInput({ placeholder: 请输入要合成的语音文本 }).width(70%).height(40).onChange((value: string) {this.text value})Button(合成).fontSize(16).width(25%).height(40).margin({ left: 10 }).onClick(() {this.download()})}.height(100%).padding({ bottom: 10 }).alignItems(VerticalAlign.Bottom)}async download() {if (this.text )returnpromptAction.showToast({ message: 合成文本 this.text })let httpRequest http.createHttp();let context getContext(this) as common.UIAbilityContext;const filesDir context.filesDir;let promise httpRequest.request(this.ttsUrl, {method: http.RequestMethod.POST,header: { Content-Type: application/json; charsetutf-8 },extraData: { text: this.text }})promise.then((data) {const timestamp Date.now();const savePath filesDir /${timestamp}.wavconsole.info(保存路径 savePath)let file fs.openSync(savePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);// ts-ignorefs.write(file.fd, data.result).then((writeLen) {fs.closeSync(file);console.info(已成功保存文件文件大小为 writeLen);}).catch((err) {console.error(保存文件出错错误信息 err.message , 错误代码 err.code);});}).catch((err) {console.error(错误信息 JSON.stringify(err))})}
}