前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >鸿蒙应用开发-请求语音合成服务获取音频文件

鸿蒙应用开发-请求语音合成服务获取音频文件

作者头像
夜雨飘零
发布2024-05-26 16:09:21
490
发布2024-05-26 16:09:21
举报
文章被收录于专栏:CSDN博客CSDN博客

功能介绍:

请求语音合成服务,通过上传语音合成文本,返回音频数据,并保存到本地。这里要说明一下,由于HttpResponse接口给问题,服务的响应类型必须是application/octet-stream,才能正确获取音频数据并保存,接口文档:HttpResponse

语音合成服务可以参考:轻松快速搭建一个本地的语音合成服务

使用环境:

  • API 9
  • DevEco Studio 4.0 Release
  • Windows 11
  • Stage模型
  • ArkTS语言

所需权限:

  1. ohos.permission.INTERNET
  2. 只保存在应用文件夹,不涉及额外目录,不需要申请读写权限

注意: 只适合小于5M数据。

关键代码片段如下:

代码语言:javascript
复制
  async download() {
  if (this.text == "")return
  promptAction.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; charset=utf-8' },
    extraData: { "text": this.text }
  })
  promise.then((data) => {
    const timestamp = Date.now();
    const savePath = filesDir + `/${timestamp}.wav`
    console.info("保存路径:" + 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("已成功保存文件,文件大小为:" + writeLen);
    }).catch((err) => {
      console.error("保存文件出错,错误信息:" + err.message + ", 错误代码:" + err.code);
    });
  }).catch((err) => {
    console.error('错误信息:' + JSON.stringify(err))
  })
}

完整代码:

代码语言:javascript
复制
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: '请输入要合成的语音文本' })
        .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 == "")return
    promptAction.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; charset=utf-8' },
      extraData: { "text": this.text }
    })
    promise.then((data) => {
      const timestamp = Date.now();
      const savePath = filesDir + `/${timestamp}.wav`
      console.info("保存路径:" + 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("已成功保存文件,文件大小为:" + writeLen);
      }).catch((err) => {
        console.error("保存文件出错,错误信息:" + err.message + ", 错误代码:" + err.code);
      });
    }).catch((err) => {
      console.error('错误信息:' + JSON.stringify(err))
    })
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
语音合成
语音合成(Text To Speech,TTS)满足将文本转化成拟人化语音的需求,打通人机交互闭环。提供多场景、多语言的音色选择,支持 SSML 标记语言,支持自定义音量、语速等参数,让发音更专业、更符合场景需求。语音合成广泛适用于智能客服、有声阅读、新闻播报、人机交互等业务场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档