melonjs-audio
하는 일
melonJS 게임에서 오디오를 설정하고, 소리를 불러오고, 올바르게 재생하는 방법을 설명합니다. 브라우저 게임에 사운드를 추가하는 게임 개발자를 위한 스킬입니다.
사용하는 경우
- ✓개발자가 melonJS 게임에 효과음이나 배경 음악을 추가하려 할 때.
- ✓개발자가 소리를 불러올 때 오디오 형식 오류를 만날 때.
- ✓개발자가 오브젝트의 위치에 따라 소리가 페이드되거나 좌우로 이동하게 하려 할 때.
스킬 링크
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-audio/SKILL.md복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.
사용 방법
가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.
바로 붙여넣을 수 있는 메시지
Claude Code, Codex, Cursor용입니다. 프로젝트 폴더에서 실행하면 스킬이 설치됩니다.
SKILL.md를 다운로드해 에이전트의 스킬 폴더에 넣으세요.
AI에게 요청하는 예시
“제 melonJS 게임에 점프 효과음과 반복 재생되는 배경 음악을 추가해 주세요.”
매주 새 스킬 5개. 읽는 데 1분.
작동 방식
스킬은 AI에게 먼저 오디오 형식을 설정하고, 사운드 폴더를 미리 로드한 뒤, 효과음과 배경 트랙을 각각 별도의 호출로 처리하도록 안내합니다.
기술 내용 (원본 SKILL.md)
작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.
Audio
Two setup rules
import { audio, loader } from "melonjs";
audio.init("mp3,ogg"); // 1. BEFORE preloading any audio
await loader.preload([
{ name: "cling", type: "audio", src: "data/sfx/" }, // 2. a PREFIX, not a file
]);
audio.init(format)must run before any audio asset is preloaded, and lists the formats you actually ship. Preloading first throwstarget audio extension(s) should be set through me.audio.init() before calling the preloader.It returnsfalsewhen no audio backend is available, so it is worth checking. The format list defaults to"mp3".srcis a prefix, not a file. The loader appends the assetnameplus each configured extension and uses the first that loads — sodata/sfx/+cling+mp3→data/sfx/cling.mp3. Passing a full filename gives you a 404. Adata:audio/...URL is the one exception: it is used verbatim, prefix and extension skipped.
audio.hasAudio() says whether the browser can play audio at all, and
audio.hasFormat("ogg") whether it can play one codec — useful for choosing
the init list or hiding a sound toggle on a device with no audio.
Order matters — melonJS tries the listed formats left to right, so put the
preferred one first. Two formats ("webm,mp3" or "mp3,ogg") is the usual
belt-and-braces for codec coverage. Accepted tokens: mp3, mpeg, opus,
ogg, oga, wav, aac, caf, m4a, m4b, mp4, weba, webm, dolby,
flac.
Effects versus music
audio.play("cling"); // fire and forget
audio.play("cling", false, null, 0.5); // loop, onend, volume
const id = audio.play("engine", true); // looping; keep the id to stop it
audio.playTrack("theme"); // background music
audio.stopTrack();
Use playTrack for music, not play. playTrack(name, volume?) always
loops, and it records the name as the current track — which is what
app.pauseOnBlur / resumeOnFocus / stopOnBlur act on, via
state.pause(true) → pauseTrack(). stopTrack(), pauseTrack(),
resumeTrack() and getCurrentTrack() all read that same single slot, so there
is only ever one current track name.
playTrack stops the track it replaces (20.9.0 and later; before that it
only assigned the slot, and the previous bed kept looping underneath where
stopTrack() could no longer reach it). So a scene just states its own music
and nothing cancels anything:
class PlayScreen extends Stage {
onResetEvent() { audio.playTrack("battle", 0.6); }
}
Calling it with the track already playing restarts it, the same as play()
on any other clip. A scene that wants the bed to carry on across a screen
change should not call it again, rather than rely on it doing nothing.
stopTrack() is for where nothing follows: a scene whose successor has no
music of its own, or a game going quiet. state.change destroys the outgoing
stage before it resets the incoming one, so a stopTrack() in
onDestroyEvent and a playTrack() in the next onResetEvent are in that
order and never cancel each other.
To end a track on an event rather than on a screen change, say a run ending
while the player stays on the same screen, audio.fade(name, from, 0, ms)
takes it down gently. A fade to zero is silence, not a stop, so the voice is
still live until something stops it or replaces it.
Every per-clip function throws audio clip <name> does not exist if the clip was
never loaded, and play additionally throws when a named sprite region is
missing.
Audio sprites
One file carrying many effects, with named regions in milliseconds:
await loader.preload([{
name: "sfx", type: "audio", src: "data/sfx/",
sprite: { jump: [0, 450], hit: [2000, 250], music: [4000, 12000, true] },
}]);
audio.play("sfx", { sprite: "jump" });
A loop with no gap
An MP3 encoder pads the end of its file. Chrome and Firefox trim the padding when they decode it, Safari keeps it, so a looping MP3 has a short silence at every loop there (27 ms on a 12 s clip at 22 kHz, measured). Declare a region of the loop's exact length and loop on that: the voice never reaches the padding.
await loader.preload([{
name: "ambience", type: "audio", src: "data/sfx/",
sprite: { loop: [0, 12000] },
}]);
audio.playTrack("ambience", { sprite: "loop", volume: 0.6 });
// or, as a plain clip: audio.play("ambience", { sprite: "loop", loop: true })
자주 묻는 질문
오디오를 불러올 때 왜 오류가 나나요?
어떤 오디오도 미리 로드하기 전에 오디오 형식을 먼저 설정해야 한다고 스킬은 말합니다.
소리 원본은 파일인가요, 폴더인가요?
접두사 폴더 경로이며, 로더가 이름과 확장자를 붙입니다.
기기에서 오디오가 작동하는지 확인할 수 있나요?
네. 오디오 사용 가능 여부와 지원되는 코덱을 알려 주는 함수를 언급합니다.
상세 정보
작성자의 원본 설명
Use this skill for sound and music in melonJS — loading audio, playing effects, background tracks, audio sprites, volume and muting, spatial audio, procedural tone/noise generation, and effects such as reverb or a compressor on the whole mix. Covers the mandatory audio.init format list, the directory-not-file src convention, playTrack versus play, the fixed listener and pixel-scale distances of positional sound, and browser autoplay unlocking. Triggers on: audio, sound, audio.init, audio.play, playTrack, stopTrack, audio.load, sfx, music, volume, mute, fade, rate, pitch, sprite audio, stereo,
repo: melonjs/melonJS
인기도
- GitHub 스타
- 6.4k
- 순위
- #14197
- 라이선스
- MIT
- 업데이트
- 2026. 11. 2.
댓글
- …
이 저장소의 다른 스킬
melonjs-20-migration
6.4k에이전트가 melonJS 20 버전에 맞는 게임 코드를 작성하고, 필수 비동기 시작 방식 등 이전 버전의 오래된 메서드를 피하도록 돕습니다.
melonjs-3d-assets
6.4k에이전트가 glTF, GLB, OBJ 같은 3D 모델 파일을 melonJS 브라우저 게임에 불러오고, 크기와 조명 옵션을 설정하도록 돕습니다.
melonjs-3d
6.4k에이전트가 melonJS 브라우저 게임에서 카메라, 메시, 조명, 깊이를 포함한 3D 또는 2.5D 장면을 엔진 고유의 축 규칙에 맞게 만들도록 돕습니다.
melonjs-camera-and-drawing
6.4k에이전트가 플레이어를 따라가는 등 melonJS 게임 카메라를 제어하고, 게임 안에 사용자 정의 도형과 선을 그리도록 돕습니다.