1
0
Fork 0
mirror of synced 2024-07-13 18:26:06 +12:00

Merge pull request #11164 from Budibase/budi-7167-add-a-sound-alert-to-qr-scanner-component

add scanner beep on successful qr/barcode scan
This commit is contained in:
Andrew Thompson 2023-07-10 11:12:08 +01:00 committed by GitHub
commit eb6590be74
3 changed files with 76 additions and 0 deletions

View file

@ -3172,6 +3172,46 @@
"key": "allowManualEntry", "key": "allowManualEntry",
"defaultValue": false "defaultValue": false
}, },
{
"type": "boolean",
"label": "Play sound on scan",
"key": "beepOnScan",
"defaultValue": false
},
{
"type": "select",
"label": "Sound pitch",
"key": "beepFrequency",
"dependsOn": "beepOnScan",
"defaultValue": 2637,
"options": [
{
"label": "Low",
"value": 2096
},
{
"label": "Regular",
"value": 2637
},
{
"label": "High",
"value": 3136
},
{ "label": "Custom", "value": "custom" }
]
},
{
"type": "number",
"label": "Sound frequency (Hz)",
"key": "customFrequency",
"defaultValue": 1046,
"min": 20,
"max": 8000,
"dependsOn": {
"setting": "beepFrequency",
"value": "custom"
}
},
{ {
"type": "validation/string", "type": "validation/string",
"label": "Validation", "label": "Validation",

View file

@ -8,6 +8,10 @@
export let disabled = false export let disabled = false
export let allowManualEntry = false export let allowManualEntry = false
export let scanButtonText = "Scan code" export let scanButtonText = "Scan code"
export let beepOnScan = false
export let beepFrequency = 2637
export let customFrequency = 1046
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let videoEle let videoEle
@ -21,8 +25,13 @@
fps: 25, fps: 25,
qrbox: { width: 250, height: 250 }, qrbox: { width: 250, height: 250 },
} }
const audioCtx = new (window.AudioContext || window.webkitAudioContext)()
const onScanSuccess = decodedText => { const onScanSuccess = decodedText => {
if (value != decodedText) { if (value != decodedText) {
if (beepOnScan) {
beep()
}
dispatch("change", decodedText) dispatch("change", decodedText)
} }
} }
@ -84,6 +93,27 @@
} }
camModal.hide() camModal.hide()
} }
const beep = () => {
const oscillator = audioCtx.createOscillator()
const gainNode = audioCtx.createGain()
oscillator.connect(gainNode)
gainNode.connect(audioCtx.destination)
const frequency =
beepFrequency === "custom" ? customFrequency : beepFrequency
oscillator.frequency.value = frequency
oscillator.type = "square"
const duration = 420
const endTime = audioCtx.currentTime + duration / 1000
gainNode.gain.setValueAtTime(1, audioCtx.currentTime)
gainNode.gain.exponentialRampToValueAtTime(0.001, endTime)
oscillator.start()
oscillator.stop(endTime)
}
</script> </script>
<div class="scanner-video-wrapper"> <div class="scanner-video-wrapper">

View file

@ -11,6 +11,9 @@
export let onChange export let onChange
export let allowManualEntry export let allowManualEntry
export let scanButtonText export let scanButtonText
export let beepOnScan
export let beepFrequency
export let customFrequency
let fieldState let fieldState
let fieldApi let fieldApi
@ -42,6 +45,9 @@
disabled={fieldState.disabled} disabled={fieldState.disabled}
{allowManualEntry} {allowManualEntry}
scanButtonText={scanText} scanButtonText={scanText}
{beepOnScan}
{beepFrequency}
{customFrequency}
/> />
{/if} {/if}
</Field> </Field>