import { Howler , Howl } from 'howler' ; import { BleepsAudioGroupSettings , BleepPlayerSettings , BleepGenericInstanceId , BleepGeneric } from '../../constants' ; const createBleep = ( audioSettings , playerSettings ) : BleepGeneric => { const { disabled , ... settings } = { ... audioSettings , ... playerSettings } ; const isGlobalAudioLocked = ! ( Howler as any ) . _audioUnlocked ; let isLocked = isGlobalAudioLocked ; let lastId ; const howl = new Howl ( { ... settings , onunlock : ( ) => { isLocked = false ; } } ) ; const sourcesAccount = { } ; const play = ( instanceId ) : void => { if ( howl . state ( ) === 'unloaded' ) { howl . load ( ) ; } if ( isLocked ) { return ; } sourcesAccount [ instanceId ] = true ; const newId = howl . play ( lastId ) ; lastId = newId || undefined ; } ; const stop = ( instanceId ) : void => { delete sourcesAccount [ instanceId ] ; const noActiveSources = ! Object . keys ( sourcesAccount ) . length ; const canStop = settings . loop ? noActiveSources : true ; if ( canStop && howl . playing ( ) ) { howl . stop ( ) ; } } ; const getIsPlaying = ( ) : boolean => { return howl . playing ( ) ; } ; const getDuration = ( ) : number => { return howl . duration ( ) ; } ; const unload = ( ) : void => { howl . unload ( ) ; } ; return { _settings : settings , _howl : howl , play , stop , getIsPlaying , getDuration , unload } ; } ; export { createBleep } ;