-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundButton.vue
117 lines (96 loc) · 3.63 KB
/
SoundButton.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<template>
<div
v-on:click="activateButton()"
class="cursor-pointer hover:opacity-75 font-bold w-1/3 p-3 md:w-1/6 lg:w-1/6 lg:w-1/12 xl:w-1/12 mx-2 xl:mx-3 mb-4 h-24 rounded text-white text-sm md:text-base lg:text-sm text-center bg-black opacity-50 shadow-2xl border-yellow-200">
<span v-if="playState === 'default'">
{{buttonCopy}}
</span>
<span v-if="playState === 'active'">
<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>
</span>
</div>
</template>
<script>
import {groups, groupTypes} from '@/groups';
import {getStorageName} from "@/utils/utils.js";
const PLAY_STATE_DEFAULT = 'default';
const PLAY_STATE_ACTIVE = 'active';
export default {
name: 'SoundButton',
props: [
'text',
'filename',
'type',
],
data() {
return {
audio: null,
buttonCopy: this.text,
playState: PLAY_STATE_DEFAULT,
}
},
methods: {
activateButton: function () {
// If the audio is already playing, return.
if (this.playState === PLAY_STATE_ACTIVE) {
return;
}
this.playState = PLAY_STATE_ACTIVE;
this.playAudio(
this.getPath(this.type, this.filename, this.text)
)
.catch(this.handleError)
.finally(this.resetButton)
},
resetButton: function () {
this.playState = PLAY_STATE_DEFAULT
},
handleError: function () {
alert('Error playing sound. Please try again later.')
},
playAudio: function (path) {
return new Promise(function (resolve, reject) {
let audio = new Audio();
audio.preload = 'auto';
audio.autoplay = true;
audio.onerror = reject;
audio.onended = resolve;
audio.src = path;
});
},
getPath: function (type, filename, text) {
// todo - refactor
if (type === groupTypes.RANDOM) {
const sound = this.getRandomSound();
type = groupTypes.UNIT;
filename = sound.filename;
text = sound.label;
}
const prefix = 'https://assets.eesoundboard.online/file/ee-soundboard/';
let dir;
let extension;
switch (type) {
case groupTypes.UNIT:
dir = prefix + 'units/';
extension = '.wav';
break;
case groupTypes.MUSIC:
dir = prefix + 'music/';
extension = '.mp3';
break;
case groupTypes.DIALOG:
dir = prefix + 'dialog/';
extension = '.mp3';
break;
}
let fileName = filename ? filename : getStorageName(text);
return dir + fileName + extension;
},
getRandomSound: function () {
const sounds = groups.units.sounds;
console.log(sounds[Math.floor(Math.random() * sounds.length)]);
return sounds[Math.floor(Math.random() * sounds.length)];
},
},
}
</script>