Next2D Playerは、ゲームやアプリケーションで必要な音声機能を提供します。BGM、効果音、ボイスなど様々な用途に対応しています。
classDiagram
EventDispatcher <|-- Sound
class Sound {
+audioBuffer: AudioBuffer
+volume: Number
+loopCount: Number
+load(request): Promise
+play(startTime): void
+stop(): void
+clone(): Sound
}
class SoundMixer {
+volume: Number
+stopAll(): void
}
音声ファイルを読み込み再生するクラスです。EventDispatcherを継承しています。
| プロパティ | 型 | デフォルト | 読み取り専用 | 説明 |
|---|---|---|---|---|
audioBuffer | AudioBuffer | null | null | - | オーディオバッファ。load()で読み込んだ音声データが格納されます |
loopCount | number | 0 | - | ループ回数の設定。0でループなし、9999で実質無限ループ |
volume | number | 1 | - | ボリューム。範囲は0(無音)〜1(フルボリューム)。SoundMixer.volumeの値を超えることはできません |
canLoop | boolean | - | ○ | サウンドがループするかどうかを示します |
| メソッド | 戻り値 | 説明 |
|---|---|---|
clone() | Sound | Soundクラスを複製します。volume、loopCount、audioBufferがコピーされます |
load(request: URLRequest) | Promise<void> | 指定したURLから外部MP3ファイルのロードを開始します |
play(startTime: number = 0) | void | サウンドを再生します。startTimeは再生開始時間(秒単位)です。既に再生中の場合は何もしません |
stop() | void | チャンネルで再生しているサウンドを停止します |
const { Sound } = next2d.media;
const { URLRequest } = next2d.net;
// Soundオブジェクトを作成
const sound = new Sound();
// 音声ファイルを非同期で読み込み
const request = new URLRequest("bgm.mp3");
await sound.load(request);
// 再生開始
sound.play();
const { Sound } = next2d.media;
const { URLRequest } = next2d.net;
// 効果音をプリロード
const seJump = new Sound();
const seHit = new Sound();
const seCoin = new Sound();
// 読み込み
await seJump.load(new URLRequest("se/jump.mp3"));
await seHit.load(new URLRequest("se/hit.mp3"));
await seCoin.load(new URLRequest("se/coin.mp3"));
// 再生関数
function playSE(sound) {
// 複製して再生(同時に複数回鳴らす場合)
const clone = sound.clone();
clone.play();
}
// ゲーム中で使用
player.addEventListener("jump", () => {
playSE(seJump);
});
const { Sound } = next2d.media;
const { URLRequest } = next2d.net;
const bgm = new Sound();
// 読み込み
await bgm.load(new URLRequest("bgm/stage1.mp3"));
// 音量を設定
bgm.volume = 0.7; // 70%
// ループ回数を設定(9999で実質無限ループ)
bgm.loopCount = 9999;
// 再生
bgm.play();
// BGM停止
function stopBGM() {
bgm.stop();
}
const { Sound } = next2d.media;
const { URLRequest } = next2d.net;
const bgm = new Sound();
await bgm.load(new URLRequest("bgm.mp3"));
// 音量を設定
bgm.volume = 1.0;
bgm.play();
// 音量を変更
function setVolume(volume) {
bgm.volume = Math.max(0, Math.min(1, volume));
}
// フェードアウト
async function fadeOut(duration = 1000) {
const startVolume = bgm.volume;
const startTime = Date.now();
return new Promise((resolve) => {
const fade = () => {
const elapsed = Date.now() - startTime;
const progress = Math.min(1, elapsed / duration);
bgm.volume = startVolume * (1 - progress);
if (progress >= 1) {
bgm.stop();
resolve();
} else {
requestAnimationFrame(fade);
}
};
fade();
});
}
const { Sound, SoundMixer } = next2d.media;
const { URLRequest } = next2d.net;
class SoundManager {
constructor() {
this._sounds = new Map();
this._bgm = null;
this._bgmVolume = 0.7;
this._seVolume = 1.0;
this._isMuted = false;
}
// サウンドをプリロード
async preload(id, url) {
const sound = new Sound();
await sound.load(new URLRequest(url));
this._sounds.set(id, sound);
}
// BGM再生
playBGM(id, loops = 9999) {
this.stopBGM();
const sound = this._sounds.get(id);
if (sound) {
this._bgm = sound.clone();
this._bgm.volume = this._isMuted ? 0 : this._bgmVolume;
this._bgm.loopCount = loops;
this._bgm.play();
}
}
// BGM停止
stopBGM() {
if (this._bgm) {
this._bgm.stop();
this._bgm = null;
}
}
// SE再生
playSE(id) {
const sound = this._sounds.get(id);
if (sound) {
const clone = sound.clone();
clone.volume = this._isMuted ? 0 : this._seVolume;
clone.play();
}
}
// ミュート切り替え
toggleMute() {
this._isMuted = !this._isMuted;
if (this._bgm) {
this._bgm.volume = this._isMuted ? 0 : this._bgmVolume;
}
return this._isMuted;
}
// BGM音量設定
setBGMVolume(volume) {
this._bgmVolume = Math.max(0, Math.min(1, volume));
if (this._bgm && !this._isMuted) {
this._bgm.volume = this._bgmVolume;
}
}
// SE音量設定
setSEVolume(volume) {
this._seVolume = Math.max(0, Math.min(1, volume));
}
}
// 使用例
const soundManager = new SoundManager();
// 起動時にプリロード
async function initSounds() {
await soundManager.preload("bgm_title", "bgm/title.mp3");
await soundManager.preload("bgm_stage1", "bgm/stage1.mp3");
await soundManager.preload("se_jump", "se/jump.mp3");
await soundManager.preload("se_coin", "se/coin.mp3");
await soundManager.preload("se_damage", "se/damage.mp3");
}
// ゲーム中
soundManager.playBGM("bgm_stage1");
soundManager.playSE("se_jump");
全体のサウンドを制御するクラスです。
const { SoundMixer } = next2d.media;
// 全ての音声を停止
SoundMixer.stopAll();
// 全体の音量を変更
SoundMixer.volume = 0.5; // 50%
| フォーマット | 拡張子 | 対応状況 |
|---|---|---|
| MP3 | .mp3 | 推奨 |
| AAC | .m4a, .aac | 対応 |
| Ogg Vorbis | .ogg | ブラウザ依存 |
| WAV | .wav | 対応(ファイルサイズ大) |