Skip to content

Commit

Permalink
[Main UI] Add support for webaudio
Browse files Browse the repository at this point in the history
Fixes openhab#743

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn committed Jun 21, 2022
1 parent dd47c79 commit 8554e23
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.ui/web/build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {
// poll: 1000,
// },
proxy: [{
context: ['/auth', '/rest', '/chart', '/proxy', '/icon', '/static', '/changePassword', '/createApiToken'],
context: ['/auth', '/rest', '/chart', '/proxy', '/icon', '/static', '/changePassword', '/createApiToken', '/audio'],
target: apiBaseUrl
}]
},
Expand Down
46 changes: 46 additions & 0 deletions bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,50 @@ export default {
ev.stopPropagation()
ev.preventDefault()
}
},
startEventSource () {
this.eventSource = this.$oh.sse.connect('/rest/events?topics=openhab/webaudio/playurl', null, (event) => {
const topicParts = event.topic.split('/')
switch (topicParts[2]) {
case 'playurl':
this.playAudioUrl(JSON.parse(event.payload))
break
}
}, () => {
// in case of error, try reloading to refresh
this.stopEventSource()
this.startEventSource()
})
},
stopEventSource () {
this.$oh.sse.close(this.eventSource)
this.eventSource = null
},
playAudioUrl (audioUrl) {
let context
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext
if (typeof (window.AudioContext) !== 'undefined') {
context = new AudioContext()
}
console.log('Playing audio URL: ' + audioUrl)
this.$oh.api.getPlain(audioUrl, '', '*/*', 'arraybuffer').then((data) => {
context.decodeAudioData(data, function (buffer) {
let source = context.createBufferSource()
source.buffer = buffer
source.connect(context.destination)
source.onended = function () {
context.close()
}
source.start(0)
})
})
} catch (e) {
console.warn('Error while playing audio URL: ' + e.toString())
if (context) {
context.close()
}
}
}
},
created () {
Expand Down Expand Up @@ -656,6 +700,8 @@ export default {
if (window) {
window.addEventListener('keydown', this.keyDown)
}
this.startEventSource()
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions bundles/org.openhab.ui/web/src/js/openhab/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export default {
get (uri, data) {
return wrapPromise(Framework7.request.promise.json(uri, data))
},
getPlain (uri, data, contentType) {
getPlain (uri, data, contentType, responseType) {
return wrapPromise(Framework7.request.promise({
method: 'GET',
url: uri,
data,
processData: false,
contentType: contentType || 'text/plain'
contentType: contentType || 'text/plain',
xhrFields: typeof responseType !== 'undefined' ? { responseType: responseType } : null
}))
},
post (uri, data, dataType) {
Expand Down

0 comments on commit 8554e23

Please sign in to comment.