Skip to content

Commit

Permalink
Add support for webaudio (#1422)
Browse files Browse the repository at this point in the history
Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored Jul 9, 2022
1 parent 34c59ec commit 21490fc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"about.miscellaneous.home.background": "Standard home page background color",
"about.miscellaneous.home.hideChatInput": "Hide chat input box on home page",
"about.miscellaneous.home.disableCardExpansionAnimation": "Disable card expansion animations",
"about.miscellaneous.theme.disablePageTransition": "Disable page transition animations"
"about.miscellaneous.theme.disablePageTransition": "Disable page transition animations",
"about.miscellaneous.webaudio.enable": "Enable Web Audio sink support"
}
45 changes: 45 additions & 0 deletions bundles/org.openhab.ui/web/src/components/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export default {
return {
init: false,
ready: false,
eventSource: null,
// Framework7 Parameters
f7params: {
Expand Down Expand Up @@ -555,6 +556,46 @@ 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
}
})
},
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 +697,10 @@ export default {
if (window) {
window.addEventListener('keydown', this.keyDown)
}
if (localStorage.getItem('openhab.ui:webaudio.enable') === 'enabled') {
this.startEventSource()
}
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions bundles/org.openhab.ui/web/src/components/theme-switcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<span v-t="'about.miscellaneous.theme.disablePageTransition'" />
<f7-toggle :checked="pageTransitionAnimation === 'disabled'" @toggle:change="setPageTransitionAnimation" />
</f7-list-item>
<f7-list-item>
<span v-t="'about.miscellaneous.webaudio.enable'" />
<f7-toggle :checked="webAudio === 'enabled'" @toggle:change="setWebAudio" />
</f7-list-item>
</f7-list>
</f7-col>
</f7-row>
Expand Down Expand Up @@ -119,6 +123,10 @@ export default {
setPageTransitionAnimation (value) {
localStorage.setItem('openhab.ui:theme.pagetransition', (value) ? 'disabled' : 'default')
location.reload()
},
setWebAudio (value) {
localStorage.setItem('openhab.ui:webaudio.enable', (value) ? 'enabled' : 'default')
location.reload()
}
},
computed: {
Expand All @@ -145,6 +153,9 @@ export default {
},
pageTransitionAnimation () {
return localStorage.getItem('openhab.ui:theme.pagetransition') || 'default'
},
webAudio () {
return localStorage.getItem('openhab.ui:webaudio.enable') || 'default'
}
}
}
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 21490fc

Please sign in to comment.