> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# 音乐流式传输

> **Note**
>
> **操作指南** · 假设你已完成[音乐 快速入门](/docs/zh/eleven-api/guides/cookbooks/music)。

> **Info**
>
> Music API 仅面向付费用户开放。

## 使用 Music API

> **Note**
>
> 本指南假设你已[设置 API 密钥和 SDK](/docs/zh/eleven-api/quickstart)。如尚未完成，请先完成
> 快速入门。

#### 发起 API 请求

根据所选语言，新建名为 `example.py` 或 `example.mts` 的文件，并添加以下代码：

```python
# example.py
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
import os
from io import BytesIO
from dotenv import load_dotenv
load_dotenv()

elevenlabs = ElevenLabs(
    api_key=os.getenv("ELEVENLABS_API_KEY"),
)

stream = elevenlabs.music.stream(
    prompt="Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
    music_length_ms=10000,
    model_id="music_v2_5",
)

# Create a BytesIO object to hold the audio data in memory
audio_stream = BytesIO()

# Write each chunk of audio data to the stream
for chunk in stream:
    if chunk:
        audio_stream.write(chunk)

# Reset stream position to the beginning
audio_stream.seek(0)

play(audio_stream)
```

```typescript
// example.mts
import { ElevenLabsClient, play } from "@elevenlabs/elevenlabs-js";
import "dotenv/config";

const elevenlabs = new ElevenLabsClient();

const stream = await elevenlabs.music.stream({
  prompt: "Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
  musicLengthMs: 10000,
  modelId: "music_v2_5",
});

const chunks: Buffer[] = [];

for await (const chunk of stream) {
    chunks.push(chunk);
}

const audioStream = Buffer.concat(chunks);

await play(audioStream);
```

#### 执行代码

```python
python example.py
```

```typescript
npx tsx example.mts
```

你应该会听到生成的音乐开始播放。

## 后续步骤

#### [编曲计划](/docs/zh/eleven-api/guides/how-to/music/composition-plans)

使用编曲计划来组织并控制音乐的生成方式。

#### [音乐局部重绘](/docs/zh/eleven-api/guides/how-to/music/inpainting)

使用新生成的内容编辑并融合现有曲目的片段。