You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js调用Wit AI语音接口遇空响应问题求助

问题分析与解决方案

问题现象

调用Wit AI语音接口时,本地录制的音频可正常识别,但浏览器录制或通过WPP Connect API获取的音频返回空响应:

{
    "entities": {},
    "intents": [],
    "text": "",
    "traits": {}
}

已确认这类音频本身包含有效内容,且问题在Postman中可复现。当前调用代码如下:

await message.downloadMedia().then(data => {
    const binaryData = Buffer.from(data.data, 'base64')

    axios
    .post('https://api.wit.ai/speech?v=20230215', binaryData, {
      headers: {
        Authorization: `Bearer ${TOKEN}`,
        'Content-Type': 'audio/ogg'
      },
      responseType: 'json'
    })
    .then(response => {
      console.log(response.data) // Resposta da API
      console.log(response.status)
      const speechResponse = SpeechResponse.create(response.data)
      const chatMessageProps = {
        message: speechResponse.text,
        type: message.type,
        chatUser: chatUser
      }
      return ChatMessageInput.create(chatMessageProps)
    })
    .catch(error => {
      console.error('Erro na requisição:', error)
    })
  })

本地通过fs.createReadStream('audio7.ogg')读写文件后调用接口,问题依旧。


排查与解决方向

1. 音频编码/格式不匹配

Wit AI语音接口对音频参数有严格要求,浏览器或WPP返回的音频可能不符合标准:

  • 要求参数:单声道(mono)、16kHz采样率、PCM编码的OGG/MP3/WAV格式
  • 解决步骤:
    1. 用音频工具(如Audacity)检查目标音频的声道、采样率、编码信息
    2. 用ffmpeg转换音频至符合要求的格式,示例命令:
      ffmpeg -i input.ogg -ac 1 -ar 16000 -codec:a pcm_s16le output.wav
      
    3. 用转换后的音频测试接口,确认是否能正常识别

2. Base64解码异常

WPP返回的data.data可能包含多余前缀或编码不标准,导致解码后音频损坏:

  • 验证解码结果:将解码后的二进制数据写入本地文件,检查是否可正常播放:
    const fs = require('fs');
    fs.writeFileSync('test_audio.ogg', binaryData);
    
  • 处理前缀问题:如果data.data带有data:audio/ogg;base64,前缀,需先移除再解码:
    const cleanBase64 = data.data.replace(/^data:audio\/ogg;base64,/, '');
    const binaryData = Buffer.from(cleanBase64, 'base64');
    

3. 请求头与发送方式校验

  • 精准设置Content-Type:若转换后的音频为WAV格式,需将请求头改为audio/wav;MP3则改为audio/mpeg,确保与实际音频格式完全匹配
  • 尝试流发送方式:改用文件流发送转换后的音频,避免二进制数据处理异常:
    const stream = fs.createReadStream('converted_audio.wav');
    axios.post('https://api.wit.ai/speech?v=20230215', stream, {
      headers: {
        Authorization: `Bearer ${TOKEN}`,
        'Content-Type': 'audio/wav'
      }
    })
    

4. 接口版本与权限检查

  • 确认使用的v=20230215版本接口是否有格式更新,可尝试切换至最新稳定版本测试
  • 检查Wit AI账号的语音识别额度是否正常(空响应通常不关联额度,但需排除此可能)

内容的提问来源于stack exchange,提问作者Bia Souza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:55:13