Skip to content

Instantly share code, notes, and snippets.

@ColinMichaels
Created October 27, 2019 00:55
Show Gist options
  • Save ColinMichaels/e48f7b53609f52ebcec0846f87ee31a8 to your computer and use it in GitHub Desktop.
Save ColinMichaels/e48f7b53609f52ebcec0846f87ee31a8 to your computer and use it in GitHub Desktop.
VueJs Plugin using Howler to clean up and easily trigger a sound from anywhere. just by passing it the sound file path.
import {Howl} from 'howler';
let AudioPlugin = {
install: function (Vue, options = {}) {
AudioPlugin.events = new Vue();
Vue.prototype.$audio = {
play(src,params = {}){
this.load(src).play();
},
load(src){
return new Howl({ src: [src]})
}
}
},
play: function(src){
this.load(src).play();
},
load: function(src){
return new Howl({ src: [src]})
}
};
export default AudioPlugin;
@ColinMichaels
Copy link
Author

ColinMichaels commented Oct 27, 2019

Usage examples:

@click:
<button @click='"$audio.play('path/sound.mp3')">Play Sound</button>
you will have to register it globally in order to use this anywhere.

let sound = AudioPlugin.load('/path/sound.mp3');
this works identical to howler just added this to only have to import one item to use it individually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment