Files
test-minimax/README.md

41 lines
1.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MiniMax Text-to-Speech API 踩坑记录
## 问题
调用 MiniMax TTS API 时,返回的音频数据无法正常播放。
## 原因
MiniMax TTS API 返回的音频数据采用 **hex十六进制编码**,而不是常见的 base64 编码。
API 响应结构:
```json
{
"data": {
"audio": "4944330400000000086a54..."
}
}
```
`49443304``ID3` 的十六进制表示,这是 MP3 文件头的标识。如果误用 base64 解码,会得到乱码,导致音频文件无法播放。
## 解决
将 hex 字符串转换为 Buffer
```typescript
const audioBuffer = Buffer.from(audioHex, "hex");
```
## 修复后的代码
```typescript
const audioHex = result.data.audio || result.data.audio_file!;
const audioBuffer = Buffer.from(audioHex, "hex");
```
## 其他注意事项
1. **成功响应没有 `code` 字段**API 请求成功时不返回 `code` 字段,只有失败时才返回。因此检查逻辑应为 `if (result.code && result.code !== 0)`
2. **环境变量**:需要设置 `MINIMAX_API_KEY`