Skip to content

Commit

Permalink
fix(sounds): harden checks on sounds playing
Browse files Browse the repository at this point in the history
- reset track to start when paused
- handle errors

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy authored and backportbot[bot] committed Oct 17, 2024
1 parent 7c4c1f1 commit a7befea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
25 changes: 9 additions & 16 deletions src/components/MediaSettings/MediaDevicesSpeakerTest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export default {
}
},
data() {
return {
isPlayingTestSound: false,
}
},
computed: {
isPlayingTestSound() {
return this.soundsStore.audioObjectsPromises.wait !== null
},
buttonLabel() {
return this.isPlayingTestSound
// TRANSLATORS Playing the test sound to check speakers
Expand All @@ -67,23 +65,18 @@ export default {
}
},
beforeDestroy() {
this.soundsStore.pauseAudio('wait')
},
methods: {
t,
playTestSound() {
if (this.isPlayingTestSound) {
this.soundsStore.pauseAudio('wait')
return
}
this.isPlayingTestSound = true
try {
} else {
this.soundsStore.playAudio('wait')
this.soundsStore.audioObjects.wait.addEventListener('ended', () => {
this.isPlayingTestSound = false
}, { once: true })
} catch (error) {
console.error(error)
this.isPlayingTestSound = false
}
},
},
Expand Down
24 changes: 22 additions & 2 deletions src/stores/sounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const useSoundsStore = defineStore('sounds', {
leave: null,
wait: null,
},
audioObjectsPromises: {
join: null,
leave: null,
wait: null,
},
}),

actions: {
Expand All @@ -51,11 +56,16 @@ export const useSoundsStore = defineStore('sounds', {
if (!this.audioObjectsCreated) {
this.initAudioObjects()
}
this.audioObjects[key].play()
this.audioObjectsPromises[key] = this.audioObjects[key].play()
this.audioObjectsPromises[key].catch(error => {
console.error(error)
})
},

pauseAudio(key) {
this.audioObjects[key]?.pause()
if (this.audioObjectsPromises[key]) {
this.audioObjects[key].pause()
}
},

/**
Expand All @@ -70,6 +80,16 @@ export const useSoundsStore = defineStore('sounds', {
const audio = new Audio(filePath)
audio.load()
audio.volume = volume

audio.addEventListener('pause', () => {
this.audioObjectsPromises[key] = null
audio.currentTime = 0
})

audio.addEventListener('ended', () => {
this.audioObjectsPromises[key] = null
})

Vue.set(this.audioObjects, key, audio)
},

Expand Down

0 comments on commit a7befea

Please sign in to comment.