Add new places; add music component; add audio module; add touch events; fixes

This commit is contained in:
obergodmar
2020-06-29 18:55:33 +03:00
parent 0b52c73cab
commit dea845a76f
53 changed files with 570 additions and 188 deletions
+3 -43
View File
@@ -1,50 +1,10 @@
interface Config {
volume?: number
}
const name = 'UIAudio'
export default class Sound {
public audio: HTMLAudioElement
private volume: number
constructor(file: string, config?: Config) {
const validateURI = (fileURI: string) => {
if (fileURI) {
return fileURI
} else {
throw Error('Requires valid URI path for "file"')
}
}
const volume = this.validateVolume(config && config.volume)
const appendAudioElement = (fileValue: string) => {
const hashFn = (str: string) => {
let hash = 0
if (str.length === 0) {
return hash
}
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash
}
return Math.abs(hash)
}
const id = `${name}-${hashFn(fileValue)}`
const audioElement = document.createElement('audio')
audioElement.id = id
audioElement.src = file
audioElement.preload = 'auto'
document.body.appendChild(audioElement)
return file
}
const audioNode = appendAudioElement(validateURI(file))
this.audio = new Audio(audioNode)
constructor(file: string, volumeValue?: number) {
const volume = this.validateVolume(volumeValue)
this.audio = new Audio(file)
this.audio.load()
this.volume = volume
}