Function Introduction:¶
Request the text-to-speech (TTS) service by uploading the text to be synthesized, retrieve the audio data, and save it locally. It should be noted that due to issues with the HttpResponse interface, the service response type must be application/octet-stream to correctly obtain and save the audio data. For the interface documentation: HttpResponse.
For details on the speech synthesis service, refer to: Easily and Quickly Build a Local Speech Synthesis Service
Usage Environment:¶
- API 9
- DevEco Studio 4.0 Release
- Windows 11
- Stage Model
- ArkTS Language
Required Permissions:¶
ohos.permission.INTERNET- Data is only saved in the application folder, without involving additional directories, so no read/write permissions are required.
Note: Only suitable for data less than 5M.
Key Code Snippet:¶
async download() {
if (this.text == "") return
promptAction.showToast({ message: "Synthesizing text: " + 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; charset=utf-8' },
extraData: { "text": this.text }
})
promise.then((data) => {
const timestamp = Date.now();
const savePath = filesDir + `/${timestamp}.wav`
console.info("Save path: " + savePath)
let file = fs.openSync(savePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
// @ts-ignore
fs.write(file.fd, data.result).then((writeLen) => {
fs.closeSync(file);
console.info("File saved successfully, file size: " + writeLen);
}).catch((err) => {
console.error("Error saving file: " + err.message + ", Error code: " + err.code);
});
}).catch((err) => {
console.error('Error information: ' + JSON.stringify(err))
})
}
Complete Code:¶
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 = ''
private ttsUrl: string = "http://xxxx.xxxx"
build() {
Row() {
TextInput({ placeholder: 'Please enter the text to synthesize' })
.width("70%")
.height(40)
.onChange((value: string) => {
this.text = value
})
Button("Synthesize")
.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 == "") return
promptAction.showToast({ message: "Synthesizing text: " + 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; charset=utf-8' },
extraData: { "text": this.text }
})
promise.then((data) => {
const timestamp = Date.now();
const savePath = filesDir + `/${timestamp}.wav`
console.info("Save path: " + savePath)
let file = fs.openSync(savePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
// @ts-ignore
fs.write(file.fd, data.result).then((writeLen) => {
fs.closeSync(file);
console.info("File saved successfully, file size: " + writeLen);
}).catch((err) => {
console.error("Error saving file: " + err.message + ", Error code: " + err.code);
});
}).catch((err) => {
console.error('Error information: ' + JSON.stringify(err))
})
}
}