<PronunciationSpeaker> reuse the sound object

This commit is contained in:
Andrea Vos 2024-01-18 19:29:45 +01:00
parent 731feaf711
commit 0d15a38e1b

View File

@ -4,7 +4,7 @@
dir="ltr" dir="ltr"
:href="pronunciationLink" :href="pronunciationLink"
target="_blank" target="_blank"
@click.prevent="pronounce()" @click.prevent="pronounce"
> >
<Icon :v="icon" /><sub v-if="name">{{ name }}</sub> <Icon :v="icon" /><sub v-if="name">{{ name }}</sub>
</a> </a>
@ -26,6 +26,7 @@ export default {
data() { data() {
return { return {
state: STATE_IDLE, state: STATE_IDLE,
sound: null,
} }
}, },
computed: { computed: {
@ -49,34 +50,35 @@ export default {
}, },
icon() { icon() {
switch (this.state) { switch (this.state) {
case STATE_IDLE:
return 'volume';
case STATE_LOADING: case STATE_LOADING:
return 'spinner fa-spin'; return 'spinner fa-spin';
case STATE_PLAYING: case STATE_PLAYING:
return 'volume-up'; return 'volume-up';
case STATE_IDLE:
default:
return 'volume';
} }
} }
}, },
methods: { methods: {
pronounce() { pronounce() {
const sound = new Audio(this.pronunciationLink); if (this.sound) {
this.sound.pause();
this.sound.currentTime = 0;
this.sound.play();
return;
}
this.sound = new Audio(this.pronunciationLink);
this.sound.addEventListener('loadeddata', () => this.sound.play());
this.sound.addEventListener('play', () => this.state = STATE_PLAYING);
this.sound.addEventListener('ended', () => this.state = STATE_IDLE);
this.sound.addEventListener('error', () => this.state = STATE_IDLE);
this.state = STATE_LOADING; this.state = STATE_LOADING;
this.sound.load();
sound.addEventListener('loadeddata', () => {
this.state = STATE_PLAYING;
sound.play();
});
sound.addEventListener('ended', () => {
this.state = STATE_IDLE;
});
sound.addEventListener('error', () => {
this.state = STATE_IDLE;
});
sound.load();
}, },
}, },
}; };