melonjs-audio
Skill này làm gì
Skill giải thích cách thiết lập âm thanh trong trò chơi melonJS, tải âm thanh và phát đúng cách. Dành cho lập trình viên trò chơi muốn thêm âm thanh vào trò chơi trình duyệt.
Khi nào nên dùng
- ✓Khi lập trình viên muốn thêm hiệu ứng âm thanh hoặc nhạc nền vào trò chơi melonJS.
- ✓Khi lập trình viên gặp lỗi về định dạng âm thanh khi tải âm thanh.
- ✓Khi lập trình viên muốn âm thanh mờ dần hoặc chuyển sang trái phải theo vị trí của một đối tượng.
Liên kết skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-audio/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Thêm âm thanh nhảy và nhạc nền lặp lại vào trò chơi melonJS của tôi.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill yêu cầu AI thiết lập định dạng âm thanh trước, rồi tải trước một thư mục âm thanh, và dùng các lệnh riêng cho hiệu ứng âm thanh và nhạc nền.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 })
Câu hỏi thường gặp
Vì sao tải âm thanh báo lỗi?
Skill nói định dạng âm thanh phải được thiết lập trước khi tải bất kỳ âm thanh nào.
Nguồn âm thanh là một file hay một thư mục?
Đó là đường dẫn thư mục có tiền tố, và trình tải sẽ tự thêm tên và phần mở rộng.
Nó có kiểm tra âm thanh có hoạt động trên thiết bị không?
Có. Nó nhắc đến các hàm báo cho biết âm thanh có sẵn hay không và những codec nào được hỗ trợ.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 6.4k
- Vị trí xếp hạng
- #14197
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
melonjs-20-migration
6.4kGiúp agent của bạn viết mã trò chơi melonJS cho phiên bản 20 và tránh các phương thức cũ, như bước khởi động bất đồng bộ bắt buộc.
melonjs-3d-assets
6.4kGiúp agent của bạn tải mô hình 3D như file glTF, GLB và OBJ vào trò chơi trình duyệt melonJS, kèm tùy chọn tỷ lệ và ánh sáng.
melonjs-3d
6.4kGiúp agent của bạn xây dựng cảnh 3D hoặc 2.5D trong trò chơi trình duyệt melonJS, gồm camera, lưới, đèn và độ sâu, theo quy tắc trục của công cụ.
melonjs-camera-and-drawing
6.4kGiúp agent của bạn điều khiển camera trò chơi melonJS, chẳng hạn bám theo người chơi, và vẽ các hình hoặc đường kẻ tùy chỉnh trong trò chơi.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.