Skip to content

Commit

Permalink
[webaudio] Allow stopping play
Browse files Browse the repository at this point in the history
Use of a dictionary to store audio sources currently playing and stop them all when asked to. A corresponding core (webaudio sink) modification is following.
Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
  • Loading branch information
dalgwen committed Nov 4, 2023
1 parent f4bbba0 commit 954355d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bundles/org.openhab.ui/web/src/components/sse-events-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default {
data () {
return {
eventSource: null,
audioContext: null
audioContext: null,
audioSources: new Map()
}
},
methods: {
Expand Down Expand Up @@ -107,6 +108,13 @@ export default {
},
playAudioUrl (audioUrl) {
try {
if (audioUrl === '') {
this.audioSources.forEach(function (value, key) {
value.stop(0)
})
this.audioSources.clear()
return
}
if (!this.audioContext) {
window.AudioContext = window.AudioContext || window.webkitAudioContext
if (typeof (window.AudioContext) !== 'undefined') {
Expand All @@ -120,6 +128,12 @@ export default {
let source = this.audioContext.createBufferSource()
source.buffer = buffer
source.connect(this.audioContext.destination)
let customId = Date.now().toString()
this.audioSources.set(customId, source)
source.onended = () => {
source.stop(0)
this.audioSources.delete(customId)
}
source.start(0)
})
})
Expand Down

0 comments on commit 954355d

Please sign in to comment.