Skip to content

开始合成流式语音合成

方法名

startStreamInputTts

用法

  • 用法如下:
js
//合成的文本列表
let sentences = this.form.text.split("\n");
module.startStreamInputTts({
  params: {
    //自定义sessionId
    sessionId: "",
    //合成的文本列表
    sentences: sentences,
    //是否保存音频文件
    saveAudio: this.form.saveAudio,
    //音频文件保存目录,可以为空
    audioDir: "/storage/emulated/0/alyNui/audio",
    //文件保存类型
    saveType: this.form.format,
    //appKey
    appKey: AppConfig.appKey,
    //token
    token: AppConfig.token,
    //url
    url: AppConfig.url,
    //debug目录
    debugDir: "/storage/emulated/0/alyNui/debug",
    //设置本地存储日志文件的最大字节数, 最大将会在本地存储2个设置字节大小的日志文件
    maxLogFileSize: 50 * 1024 * 1024,
    //开启字级别时间戳。更多使用方法,请参见https://help.aliyun.com/zh/isi/developer-reference/timestamp-overview-1?spm=a2c4g.11186623.0.0.6b2744871WHhM1
    enableSubtitle: true,
    //发音人
    voice: this.form.voice,
    //音频编码格式
    format: this.form.format,
    //是否打开内部解码器,若设置mp3/opus格式, 可enable_audio_decoder打开内部解码器, 将mp3/opus编码成pcm
    enableAudioDecoder: this.form.format == "mp3" || this.form.format == "opus",
    //音频采样率,24000,可选择8000、16000、24000、48000。
    sampleRate: 16000,
    //音量
    volume: this.form.volume,
    //朗读语速
    speechRate: this.form.speechRate,
    //朗读语调
    pitchRate: this.form.pitchRate
  },
  //成功执行的函数
  success: (res) => {
    // console.log(res)
    let data = res.data;
    if (data && data.eventName == "STREAM_INPUT_TTS_EVENT_SYNTHESIS_COMPLETE" && data.audioFilePath) {
      let audioFilePath = data.audioFilePath;
      uni.showToast({
        title: "文件保存路径:" + audioFilePath,
        icon: "none",
        duration: 5000
      })
    }
  },
  //失败执行的函数
  fail: (res) => {
    console.log(res)
  },
  //接口调用完成返回函数,不管成功还是失败都会返回
  // complete: (res) => {
  //   console.log(res)
  // }
})
js
//合成的文本列表
const sentences = text.value.split("\n");
module.startStreamInputTts({
  params: {
    //自定义sessionId
    sessionId: "",
    //合成的文本列表
    sentences: sentences,
    //是否保存音频文件
    saveAudio: saveAudio.value,
    //音频文件保存目录,可以为空
    audioDir: "/storage/emulated/0/alyNui/audio",
    //文件保存类型
    saveType: format.value,
    //appKey
    appKey: AppConfig.appKey,
    //token
    token: AppConfig.token,
    //url
    url: AppConfig.url,
    //debug目录
    debugDir: "/storage/emulated/0/alyNui/debug",
    //设置本地存储日志文件的最大字节数, 最大将会在本地存储2个设置字节大小的日志文件
    maxLogFileSize: 50 * 1024 * 1024,
    //开启字级别时间戳。更多使用方法,请参见https://help.aliyun.com/zh/isi/developer-reference/timestamp-overview-1?spm=a2c4g.11186623.0.0.6b2744871WHhM1
    enableSubtitle: true,
    //发音人
    voice: voice.value,
    //音频编码格式
    format: format.value,
    //是否打开内部解码器,若设置mp3/opus格式, 可enable_audio_decoder打开内部解码器, 将mp3/opus编码成pcm
    enableAudioDecoder: format.value == "mp3" || format.value == "opus",
    //音频采样率,24000,可选择8000、16000、24000、48000。
    sampleRate: 16000,
    //音量
    volume: volume.value,
    //朗读语速
    speechRate: speechRate.value,
    //朗读语调
    pitchRate: pitchRate.value
  },
  //成功执行的函数
  success: (res) => {
    // console.log(res)
    const data = res.data;
    const eventName = data.getString("eventName")
    const audioFilePath = data.getString("audioFilePath")
    if (eventName == "STREAM_INPUT_TTS_EVENT_SYNTHESIS_COMPLETE" && audioFilePath != null) {
      uni.showToast({
        title: "文件保存路径:" + audioFilePath,
        icon: "none",
        duration: 5000
      })
    }
  },
  //失败执行的函数
  fail: (res) => {
    console.log(res)
  },
  //接口调用完成返回函数,不管成功还是失败都会返回
  // complete: (res) => {
  //   console.log(res)
  // }
} as LevenOptions)
  • 参数说明
    参数名参数类型是否必填默认值参数描述
    paramsObject接口请求参数
    params.sessionIdString自定义sessionId
    params.sentencesString[]合成的文本列表
    params.saveAudioBooleanfalse是否保存音频文件
    params.audioDirString音频文件保存目录,可以为空
    params.saveTypeString文件保存类型,可选值:pcm wav
    params.appKeyString申请的应用appKey
    params.tokenString申请的应用token
    params.accessKeyString申请的应用accessKey
    params.accessKeySecretString申请的应用accessKeySecret
    params.stsTokenString申请的应用stsToken
    params.urlString请求地址
    params.debugDirStringdebug目录
    params.maxLogFileSizeInteger设置本地存储日志文件的最大字节数, 最大将会在本地存储2个设置字节大小的日志文件
    params.enableSubtitleBooleanfalse开启字级别时间戳。更多使用方法,请参见https://help.aliyun.com/zh/isi/developer-reference/timestamp-overview-1?spm=a2c4g.11186623.0.0.6b2744871WHhM1
    params.voiceString发音人
    params.formatString音频编码格式,可选值:pcm wav
    params.enableAudioDecoderBooleanfalse是否打开内部解码器,若设置mp3/opus格式, 可enable_audio_decoder打开内部解码器, 将mp3/opus编码成pcm
    params.sampleRateInteger16000音频采样率,默认值:16000Hz。根据音频采样率在管控台对应项目中配置支持该采样率及场景的模型,可选值:8000、16000、24000、48000
    params.volumeInteger50范围是0~100
    params.speechRateInteger0朗读语速,范围是-500~500
    params.pitchRateInteger0朗读语调,范围是-500~500
    successFunction执行成功的函数
    failFunction执行失败的函数
    completeFunction执行完成的函数,不管成功还是失败都会执行

回调

  • 示例
json
{
  "data": {
      "type": "startSuccess"
  },
  "message": "",
  "code": 0
}
json
{
  "data": {
      "type": "playStart"
  },
  "message": "",
  "code": 0
}
json
{
  "data": {
      "type": "playOver"
  },
  "message": "",
  "code": 0
}
json
{
  "data": {
      "errorMsg": "Gateway:SUCCESS:Success.",
      "taskId": "042ee4f7c4bd4fab91815c705e26644e",
      "timestamp": "",
      "allResponse": "{\"header\":{\"namespace\":\"FlowingSpeechSynthesizer\",\"name\":\"SynthesisStarted\",\"status\":20000000,\"message_id\":\"2b6b2e5bf67a4a30957ee96447fcfe33\",\"task_id\":\"042ee4f7c4bd4fab91815c705e26644e\",\"status_text\":\"Gateway:SUCCESS:Success.\"}}",
      "type": "onStreamInputTtsEventCallback",
      "eventName": "STREAM_INPUT_TTS_EVENT_SYNTHESIS_STARTED",
      "retCode": 20000000,
      "sessionId": "",
      "audioFilePath": "/storage/emulated/0/alyNui/audio/tts_task_id_042ee4f7c4bd4fab91815c705e26644e.pcm",
      "eventCode": 0
  },
  "message": "",
  "code": 0
}
  • 回调说明:
    参数名参数类型参数描述
    messageString消息提示
    dataObject数据对象
    data.typeString回调类型:
    startSuccess:开启成功
    playStart:开始播放
    playOver:结束播放
    onStreamInputTtsEventCallback:流式语音合成回调
    data.errorMsgString错误类型
    data.taskIdString任务id
    data.timestampString时间戳
    data.allResponseString响应数据
    data.eventNameString回调事件类型
    data.retCodeString-
    data.sessionIdString自定义sessionId
    data.audioFilePathString音频保存本地文件路径
    data.eventCodeString回调事件code值
    codeInteger返回类型,0.成功,其他:失败