Play Music
Play music media
Description
'play music <music_id> [with [loop] [fade <time>] [volume <percentage>] [<effects>]]'
'play music' // Resume all paused musicThe play music action lets you play background music for your game. You can play as many songs as you want simultaneously.
To stop the music, check out the Stop Music documentation.
Action ID: Play
Reversible: Yes
Requires User Interaction: No
Parameters
music_id
string
The name of the music you want to play. These assets must be declared beforehand.
loop
flag
Optional. Makes the music loop indefinitely.
fade
number
Optional. Fade in time in seconds.
volume
number
Optional. Volume percentage (0-100).
Assets Declarations
To play a song, you must first add the file to your assets/music/ directory and then declare it. To do so, Monogatari has a function that will let you declare all kinds of assets for your game.
Supported Formats
Each browser has its own format compatibility. MP3 however is the format supported by every browser.
If you wish to use other formats, you can check a compatibility table to discover what browsers will be able to play it.
Examples
Play Music
The following will play the song, and once the song ends, it will simply stop.
Loop Music
The following will play the song, and once the song ends, it will start over on an infinite loop until it is stopped using the Stop Music Action.
Fade In Effect
The following will play the song with a fade in effect.
Custom Volume
The following will set the volume of this song to 73%.
Please note that the user's preferences regarding volumes are always respected, which means this percentage is taken from the current player preferences. If the player has set the volume to 50%, the actual volume value for the song will be:
Resume Paused Music
If music has been paused using the Pause action, you can resume it:
All Together
You can combine all properties. The order doesn't matter.
Audio Effects
You can apply multiple audio effects to your music by specifying them in the properties. These effects use the Web Audio API for real-time audio processing.
Effect Syntax
Effects are specified by their name followed by their parameters:
Available Effects
Filter
Applies a Biquad filter (lowpass, highpass, etc.)
Parameters:
type- Filter type:lowpass,highpass,bandpass,notch,allpass,peaking,lowshelf,highshelf(default:lowpass)frequency- Cutoff frequency in Hz (default:800)Q- Quality factor (default:1)gain- Filter gain in dB (default:0)
Example:
Delay
A simple delay effect with feedback.
Parameters:
time- Delay time in seconds (default:0.4)feedback- Feedback amount 0-1 (default:0.5)mix- Wet/dry mix 0-1 (default:0.5)
Example:
Compressor
Dynamic range compression.
Parameters:
threshold- Compression threshold in dB (default:-24)knee- Knee width in dB (default:30)ratio- Compression ratio (default:12)attack- Attack time in seconds (default:0.003)release- Release time in seconds (default:0.25)
Example:
Tremolo
Modulates the amplitude of the signal.
Parameters:
frequency- Modulation frequency in Hz (default:5)depth- Modulation depth 0-1 (default:0.8)
Example:
Distortion
Applies wave-shaping distortion.
Parameters:
amount- Distortion amount (default:50)oversample- Oversampling:2x,4x, ornone(default:4x)
Example:
Convolution Reverb
Convolution reverb with a generated impulse response.
Parameters:
seconds- Reverb duration in seconds (default:2)decay- Decay rate (default:2)reverse- Reverse the impulse response (default:false)
Example:
Bitcrusher
Reduces bit depth and sample rate of the signal.
Parameters:
bits- Bit depth 1-16 (default:4)frequency- Sample rate reduction 0-1 (default:0.1)
Example:
AutoWah
An envelope-following filter (auto-wah).
Parameters:
baseFrequency- Base frequency in Hz (default:100)octaves- Frequency range in octaves (default:6)sensitivity- Envelope sensitivity 0-1 (default:0.5)Q- Filter Q factor (default:10)
Example:
Panner
Positions the sound in 3D space.
Parameters:
x- X position (default:0)y- Y position (default:0)z- Z position (default:0)
Example:
Phaser
A sweeping phase-shifting effect.
Parameters:
frequency- LFO frequency in Hz (default:0.5)depth- Modulation depth in Hz (default:1000)feedback- Feedback amount 0-1 (default:0.5)stages- Number of filter stages (default:4)
Example:
Chorus
Creates a thicker sound by modulating a delayed signal.
Parameters:
frequency- LFO frequency in Hz (default:1.5)delay- Delay time in seconds (default:0.025)depth- Modulation depth in seconds (default:0.002)mix- Wet/dry mix 0-1 (default:0.5)
Example:
Wah
A sweeping filter effect, like a guitar wah-wah pedal.
Parameters:
baseFrequency- Base frequency in Hz (default:350)Q- Filter Q factor (default:15)depth- Frequency sweep range in Hz (default:1500)frequency- LFO frequency in Hz (default:2)
Example:
Ring Modulation
Ring modulation for creating metallic, bell-like sounds.
Parameters:
frequency- Modulator frequency in Hz (default:30)mix- Wet/dry mix 0-1 (default:0.5)
Example:
Saturator
Soft clipping for warmth and harmonics.
Parameters:
drive- Drive amount (default:5)
Example:
Limiter
A hard compressor to prevent signal peaks from exceeding a threshold.
Parameters:
threshold- Limiting threshold in dB (default:-1.0)release- Release time in seconds (default:0.05)
Example:
Multiple Effects
You can combine multiple effects on a single music track:
Effect Compatibility
Some effects require AudioWorklet support, which may not be available in all browsers:
Bitcrusher and AutoWah require AudioWorklet support
If AudioWorklet is not available, these effects will fall back to a simple gain node (no effect)
Other effects use standard Web Audio API nodes and work in all modern browsers
Example: Complex Audio Setup
This example creates a layered audio experience with:
A main theme with low-pass filtering, delay, and looping
An ambient track with high-pass filtering and reverb
Related Actions
Stop Music - Stop playing music
Pause - Pause playing media
Last updated
Was this helpful?