ゲームエンジンと組み合わせる
Player は標準では自前のオーディオコンテキストを作り、スピーカーへ直接鳴らします。ゲームでは、エンジンのコンテキストと出力先のノードを渡しましょう。そうすれば音楽がゲームの音量、ミュート、一時停止に従います。
ts
new Player({ context, destination });| オプション | 意味 |
|---|---|
context | 共有する AudioContext。既定値:新しく作ったもの。 |
destination | 出力先の AudioNode。音楽用のバスやゲインノードなど、何でもかまいません。既定値:コンテキストのスピーカー。 |
debuff
ts
import { Audio } from "@debuffjp/audio";
import { Player } from "plonkjs";
const audio = new Audio();
const music = new Player({ context: audio.ctx, destination: audio.bus("music") });
audio.volume("music", 0.7); // the music bus
audio.volume("master", muted ? 0 : 1); // mute covers music and sound effectsPhaser
ts
const sound = this.sound as Phaser.Sound.WebAudioSoundManager;
const music = new Player({ context: sound.context, destination: sound.destination });そのほかのエンジン
エンジンが使っている AudioContext を渡し、音量の調整には自分で作ったゲインノードを使います。
ts
const context = new AudioContext();
const musicVolume = context.createGain();
musicVolume.connect(context.destination);
const music = new Player({ context, destination: musicVolume });
musicVolume.gain.value = 0.5;一時停止
ゲームを一時停止したら、音楽を止めるか、コンテキストをサスペンドします。どちらにするかは、ポーズ画面でも音楽を流し続けたいかどうかで決めましょう。
ts
player.stop(0.3); // fade out; play() starts again from the top
await context.suspend(); // or freeze everything, sound effects included, and resume() later