💻 代码示例

← 返回首页

cURL

curl -X POST 'https://edgetts.aws.xin/v1/audio/speech' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "input": "Hello world",
    "voice": "en-GB-SoniaMultilingualNeural",
    "speed": 0.7
  }' \
  --output audio.mp3

Python - 标准输出

import requests

response = requests.post(
    'https://edgetts.aws.xin/v1/audio/speech',
    headers={
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    json={
        'input': 'Hello world',
        'voice': 'en-GB-SoniaMultilingualNeural',
        'speed': 0.7
    }
)

with open('audio.mp3', 'wb') as f:
    f.write(response.content)

Python - 流式输出 ⚡

import requests

response = requests.post(
    'https://edgetts.aws.xin/v1/audio/speech',
    headers={
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    json={
        'input': 'Hello world',
        'voice': 'en-GB-SoniaMultilingualNeural',
        'speed': 0.7,
        'stream': True  # 启用流式输出
    },
    stream=True  # 重要:requests 也要启用 stream
)

with open('audio.mp3', 'wb') as f:
    for chunk in response.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)
            # 可以在这里实时处理音频数据

JavaScript (Node.js) - 标准输出

const fs = require('fs');

fetch('https://edgetts.aws.xin/v1/audio/speech', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        input: 'Hello world',
        voice: 'en-GB-SoniaMultilingualNeural',
        speed: 0.7
    })
})
.then(res => res.arrayBuffer())
.then(buffer => fs.writeFileSync('audio.mp3', Buffer.from(buffer)));

JavaScript (Node.js) - 流式输出 ⚡

const fs = require('fs');

fetch('https://edgetts.aws.xin/v1/audio/speech', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        input: 'Hello world',
        voice: 'en-GB-SoniaMultilingualNeural',
        speed: 0.7,
        stream: true  // 启用流式输出
    })
})
.then(res => {
    const fileStream = fs.createWriteStream('audio.mp3');
    res.body.pipe(fileStream);
    
    res.body.on('data', chunk => {
        console.log(`接收到 ${chunk.length} 字节`);
    });
});

JavaScript (浏览器) - 标准输出

fetch('https://edgetts.aws.xin/v1/audio/speech', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        input: 'Hello world',
        voice: 'en-GB-SoniaMultilingualNeural',
        speed: 0.7
    })
})
.then(res => res.blob())
.then(blob => {
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    audio.play();
});

JavaScript (浏览器) - 流式输出 ⚡

fetch('https://edgetts.aws.xin/v1/audio/speech', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        input: 'Hello world',
        voice: 'en-GB-SoniaMultilingualNeural',
        speed: 0.7,
        stream: true  // 启用流式输出
    })
})
.then(async res => {
    const reader = res.body.getReader();
    const chunks = [];
    
    while (true) {
        const {done, value} = await reader.read();
        if (done) break;
        
        chunks.push(value);
        console.log(`接收到 ${value.length} 字节`);
    }
    
    // 合并所有数据块
    const blob = new Blob(chunks, {type: 'audio/mpeg'});
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    audio.play();
});

PHP

$ch = curl_init('https://edgetts.aws.xin/v1/audio/speech');

curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer YOUR_API_KEY',
        'Content-Type: application/json'
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'input' => 'Hello world',
        'voice' => 'en-GB-SoniaMultilingualNeural',
        'speed' => 0.7
    ])
]);

$response = curl_exec($ch);
file_put_contents('audio.mp3', $response);
curl_close($ch);