음악 스트리밍

이 가이드에서는 Music API로 음악을 스트리밍하는 방법을 알아봅니다.

사용 방법 가이드 · 음악 빠른 시작을 완료했다고 가정합니다.

Music API는 유료 사용자만 이용할 수 있습니다.

Music API 사용하기

이 가이드는 API 키와 SDK를 설정했다고 가정합니다. 아직 완료하지 않았다면 먼저 빠른 시작을 완료하세요.

1

API 요청 보내기

선택한 언어에 따라 example.py 또는 example.mts라는 새 파일을 만들고 다음 코드를 추가하세요.

# 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)
2

코드 실행

python example.py

생성된 음악이 재생되는 것을 들을 수 있습니다.

다음 단계