mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-14 20:39:03 +00:00
uberf-8485: fix sounds (#6944)
Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
parent
171e1e88b7
commit
288c51aa0d
@ -3,10 +3,10 @@ import { type Asset, getMetadata, getResource } from '@hcengineering/platform'
|
|||||||
import { getClient } from '.'
|
import { getClient } from '.'
|
||||||
import notification from '@hcengineering/notification'
|
import notification from '@hcengineering/notification'
|
||||||
|
|
||||||
const sounds = new Map<Asset, AudioBufferSourceNode>()
|
const sounds = new Map<Asset, AudioBuffer>()
|
||||||
const context = new AudioContext()
|
const context = new AudioContext()
|
||||||
|
|
||||||
export async function prepareSound (key: string, _class?: Ref<Class<Doc>>, loop = false, play = false): Promise<void> {
|
export async function prepareSound (key: string, _class?: Ref<Class<Doc>>): Promise<void> {
|
||||||
if (_class === undefined) return
|
if (_class === undefined) return
|
||||||
|
|
||||||
const client = getClient()
|
const client = getClient()
|
||||||
@ -23,39 +23,45 @@ export async function prepareSound (key: string, _class?: Ref<Class<Doc>>, loop
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const soundUrl = getMetadata(key as Asset) as string
|
const soundUrl = getMetadata(key as Asset) as string
|
||||||
const audioBuffer = await fetch(soundUrl)
|
const rawAudio = await fetch(soundUrl)
|
||||||
.then(async (res) => await res.arrayBuffer())
|
const rawBuffer = await rawAudio.arrayBuffer()
|
||||||
.then(async (ArrayBuffer) => await context.decodeAudioData(ArrayBuffer))
|
const decodedBuffer = await context.decodeAudioData(rawBuffer)
|
||||||
|
|
||||||
|
sounds.set(key as Asset, decodedBuffer)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Sound not found', key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function playSound (
|
||||||
|
soundKey: string,
|
||||||
|
_class?: Ref<Class<Doc>>,
|
||||||
|
loop = false
|
||||||
|
): Promise<(() => void) | null> {
|
||||||
|
const soundAssetKey = soundKey as Asset
|
||||||
|
if (!sounds.has(soundAssetKey)) {
|
||||||
|
await prepareSound(soundKey, _class)
|
||||||
|
}
|
||||||
|
|
||||||
|
const sound = sounds.get(soundKey as Asset)
|
||||||
|
if (sound === undefined) {
|
||||||
|
console.error('Cannot prepare audio buffer', soundKey)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const audio = context.createBufferSource()
|
const audio = context.createBufferSource()
|
||||||
audio.buffer = audioBuffer
|
audio.buffer = sound
|
||||||
audio.loop = loop
|
audio.loop = loop
|
||||||
sounds.set(key as Asset, audio)
|
audio.connect(context.destination)
|
||||||
if (play) {
|
audio.start()
|
||||||
playSound(key)
|
|
||||||
|
return (): void => {
|
||||||
|
audio.stop()
|
||||||
|
audio.disconnect(context.destination)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('sound not found', key)
|
console.error('Error when playing sound back', soundKey, err)
|
||||||
}
|
return null
|
||||||
}
|
|
||||||
|
|
||||||
export function playSound (soundKey: string, _class?: Ref<Class<Doc>>, loop = false): void {
|
|
||||||
const sound = sounds.get(soundKey as Asset)
|
|
||||||
if (sound !== undefined) {
|
|
||||||
try {
|
|
||||||
sound.connect(context.destination)
|
|
||||||
sound.start()
|
|
||||||
} catch (err) {
|
|
||||||
console.error('error happened during sound play', soundKey, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
void prepareSound(soundKey, _class, loop, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function stopSound (soundKey: string): void {
|
|
||||||
const sound = sounds.get(soundKey as Asset)
|
|
||||||
if (sound !== undefined && sound?.context.state === 'running') {
|
|
||||||
sound.stop()
|
|
||||||
sound.disconnect(context.destination)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
import { PersonAccount, formatName } from '@hcengineering/contact'
|
import { PersonAccount, formatName } from '@hcengineering/contact'
|
||||||
import { Avatar, personByIdStore } from '@hcengineering/contact-resources'
|
import { Avatar, personByIdStore } from '@hcengineering/contact-resources'
|
||||||
import { getCurrentAccount } from '@hcengineering/core'
|
import { getCurrentAccount } from '@hcengineering/core'
|
||||||
import { getClient, playSound, stopSound } from '@hcengineering/presentation'
|
import { getClient, playSound } from '@hcengineering/presentation'
|
||||||
import { Button, Label } from '@hcengineering/ui'
|
import { Button, Label } from '@hcengineering/ui'
|
||||||
import { JoinRequest, RequestStatus } from '@hcengineering/love'
|
import { JoinRequest, RequestStatus } from '@hcengineering/love'
|
||||||
import love from '../plugin'
|
import love from '../plugin'
|
||||||
@ -29,6 +29,7 @@
|
|||||||
$: person = $personByIdStore.get(request.person)
|
$: person = $personByIdStore.get(request.person)
|
||||||
|
|
||||||
const client = getClient()
|
const client = getClient()
|
||||||
|
let stopSound: (() => void) | null = null
|
||||||
|
|
||||||
async function accept (): Promise<void> {
|
async function accept (): Promise<void> {
|
||||||
await client.update(request, { status: RequestStatus.Approved })
|
await client.update(request, { status: RequestStatus.Approved })
|
||||||
@ -43,11 +44,13 @@
|
|||||||
async function decline (): Promise<void> {
|
async function decline (): Promise<void> {
|
||||||
await client.update(request, { status: RequestStatus.Rejected })
|
await client.update(request, { status: RequestStatus.Rejected })
|
||||||
}
|
}
|
||||||
onMount(() => {
|
|
||||||
playSound(love.sound.Knock, love.class.JoinRequest, true)
|
onMount(async () => {
|
||||||
|
stopSound = await playSound(love.sound.Knock, love.class.JoinRequest, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
stopSound(love.sound.Knock)
|
stopSound?.()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user