Sound plugin is one of the core plugins of AVG.js, providing audio playback.
import { core, plugins } from 'avg-core';
// run this code before core.render() to install the plugin
core.installPlugin(plugins.Audio);
// you can use it wherever you want,
// and more APIs is listed below.
core.plugins.audio.create(0, 'bgm/bgm.ogg', { loop: true });
core.plugins.audio.play(0);
channel: Channel number or channel name, which can be a number or a string
src: Relative patt to assets folder
options:
name | type | defualt | description |
---|---|---|---|
loop | boolean | false | |
preload | boolean | true | Load immediately or load when .play () is executed |
autoplay | boolean | true | |
mute | boolean | false | |
rate | number | 1 | Playback rate |
autounload | boolean | true | automatically unload audio |
exclusive | boolean | true | If false, allows multiple audio to use the same channel |
Destroy a channel (stop playing and unload audio)
Get an instance of a channel and return Howler instance
Play the audio of a channel
Pause / resume the audio of a channel
Stop the audio of a channel
Mute the audio of a channel
Gets / sets the volume of a channel
Fade the audio volume of a channel from one value to another
Fade the audio volume of a channel to a value
Fade the audio volume of a channel to zero
Gets / sets the playback rate of a channel
Gets / sets the playback position of a channel
Gets / sets whether a channel is looped
Stop all channels of audio
Check if a channel has audio
Get the audio volume of a channel
Sets the audio volume of a channel, in the range 0-1
Gets / sets the global volume, and the final volume of each channel is equal to the product of the global volume and the channel volume
Gets / sets global mute state
You can use it like this in storyscript:
[sound bgm src='bgm/bgm.ogg' loop=true]
[sound bgm stop]
// or
[sound channel='bgm' src='bgm/bgm.ogg' loop=true]
[sound channel='bgm' stop]
Script content consists of three parts: command name (sound), channel (channel), parameters
Among them, the command name is fixed to sound
;
Channel is usually channel=<number | string>
format, but for the sake of simplicity, we added bgm
se
voice
three syntax sugar, respectively replacing channel='bgm'
channel='se'
channel='voice'
.
The following lists all supported commands (in fact they only have a small difference from the APIs above):
// the value after `=` is the default value
[sound channel= src= volume=1 loop=false preload=true autoplay=true mute=false rate=1 autounload=true exclusive=true]
[sound channel= play/pause/stop/destroy]
[sound channel= mute/rate/volume/seek/loop value=]
[sound channel= fade from= to= duration=]
[sound channel= fadein to= duration=]
[sound channel= fadeout duration=]
// global
[sound mute/volume value=]
[sound stopall]
Why can not I get a value by command?
This is related to the design ideas, we believe that storyscript should only bear the simple work. The "get" command and the subsequent processing will undoubtedly introduce more complex logic into storyscript, which we do not want to see. If you need to get some value and proccess it, you should use Javascript to do so and expose it as a new command for storyscript.