Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- part of LD35;
- class Audio {
- static Audio instance;
- AudioContext ctx;
- String extension = "ogg";
- GainNode volumeNode;
- double volume = 1.0;
- bool muted = false;
- RangeInputElement slider;
- DivElement mutebutton;
- Audio() {
- instance = this;
- this.ctx = new AudioContext();
- volumeNode = this.ctx.createGain()..connectNode(ctx.destination);
- AudioElement a = new AudioElement();
- if (a.canPlayType("audio/mpeg;codecs=mp3") != "") {
- extension = "mp3";
- };
- mutebutton = querySelector("#volume");
- slider = querySelector("#volumeslider");
- mutebutton.onClick.listen((Event e) {
- toggleMute();
- });
- slider.onChange.listen((Event e) {
- setVolume(double.parse(slider.value), slider:false);
- });
- setVolume(0.5);
- }
- static void setVolume(num vol, {bool mute : false, bool slider : true}) {
- instance.volume = clamp(vol.toDouble(), 0.0, 1.0);
- instance.volumeNode.gain.value = mute ? 0.0 : instance.volume;
- instance.muted = mute;
- if (slider) {
- instance.slider.value = "${instance.volume}";
- }
- instance.updateMuteElement();
- }
- static void play(String soundname, [String subtitle=null]) {
- if (soundname == null) { return; }
- if (Assets.sounds.containsKey(soundname)) {
- AudioBuffer sound = Assets.sounds[soundname];
- var source = instance.ctx.createBufferSource()
- ..buffer = sound
- ..connectNode(instance.volumeNode)
- ..start(0);
- if (subtitle != null) {
- }
- }
- }
- static void playRandom(List<String> soundnames, [List<String> subtitle=null]) {
- if (soundnames == null || soundnames.isEmpty) { return; }
- int r = rand.nextInt(soundnames.length);
- String sub = subtitle == null ? null : subtitle[r];
- play(soundnames[r], sub);
- }
- static void toggleMute() {
- setVolume(instance.volume, mute:!instance.muted);
- }
- void updateMuteElement() {
- if (this.muted) {
- mutebutton.classes.remove("glyphicon-volume-up");
- mutebutton.classes.add("glyphicon-volume-off");
- } else {
- mutebutton.classes.remove("glyphicon-volume-off");
- mutebutton.classes.add("glyphicon-volume-up");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement