@solid-primitives/audio
Advanced tools
Comparing version 1.3.17 to 1.3.18
@@ -38,3 +38,3 @@ import { Accessor } from 'solid-js'; | ||
* ```ts | ||
* const { start, seek } = makeAudioPlayer('./example1.mp3); | ||
* const { start, seek } = makeAudioPlayer('./example1.mp3'); | ||
* ``` | ||
@@ -89,2 +89,2 @@ */ | ||
export { AudioEventHandlers, AudioSource, AudioState, createAudio, makeAudio, makeAudioPlayer }; | ||
export { type AudioEventHandlers, type AudioSource, AudioState, createAudio, makeAudio, makeAudioPlayer }; |
@@ -18,11 +18,12 @@ import { onMount, onCleanup, createEffect } from 'solid-js'; | ||
var unwrapSource = (src) => { | ||
let player; | ||
if (src instanceof HTMLAudioElement) { | ||
player = src; | ||
} else { | ||
player = new Audio(); | ||
player[typeof src === "string" ? "src" : "srcObject"] = src; | ||
return src; | ||
} | ||
const player = new Audio(); | ||
setAudioSrc(player, src); | ||
return player; | ||
}; | ||
function setAudioSrc(el, src) { | ||
el[typeof src === "string" ? "src" : "srcObject"] = src; | ||
} | ||
var makeAudio = (src, handlers = {}) => { | ||
@@ -33,14 +34,12 @@ if (isServer) { | ||
const player = unwrapSource(src); | ||
const listeners = (enabled) => { | ||
Object.entries(handlers).forEach( | ||
([evt, handler]) => player[enabled ? "addEventListener" : "removeEventListener"]( | ||
evt, | ||
handler | ||
) | ||
); | ||
}; | ||
onMount(() => listeners(true)); | ||
onMount(() => { | ||
for (const [name, handler] of Object.entries(handlers)) { | ||
player.addEventListener(name, handler); | ||
} | ||
}); | ||
onCleanup(() => { | ||
player.pause(); | ||
listeners(false); | ||
for (const [name, handler] of Object.entries(handlers)) { | ||
player.removeEventListener(name, handler); | ||
} | ||
}); | ||
@@ -60,10 +59,10 @@ return player; | ||
const player = makeAudio(src, handlers); | ||
const play = () => player.play(); | ||
const pause = () => player.pause(); | ||
const seek = (time) => ( | ||
return { | ||
player, | ||
play: () => player.play(), | ||
pause: () => player.pause(), | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
player.fastSeek ? player.fastSeek(time) : player.currentTime = time | ||
); | ||
const setVolume = (volume) => player.volume = volume; | ||
return { play, pause, seek, setVolume, player }; | ||
seek: player.fastSeek ? (time) => player.fastSeek(time) : (time) => player.currentTime = time, | ||
setVolume: (volume) => player.volume = volume | ||
}; | ||
}; | ||
@@ -93,10 +92,11 @@ var createAudio = (src, playing, volume) => { | ||
currentTime: 0, | ||
get duration() { | ||
return this.player.duration; | ||
}, | ||
get volume() { | ||
return this.player.volume; | ||
} | ||
duration: 0, | ||
volume: 0 | ||
}); | ||
const { play, pause, setVolume, seek } = makeAudioPlayer(store.player, { | ||
const { | ||
play, | ||
pause, | ||
setVolume: _setVolume, | ||
seek | ||
} = makeAudioPlayer(store.player, { | ||
loadeddata: () => { | ||
@@ -119,11 +119,16 @@ setStore({ | ||
pause: () => setStore("state", "paused" /* PAUSED */), | ||
error: () => setStore("state", "error" /* ERROR */) | ||
error: () => setStore("state", "error" /* ERROR */), | ||
ended: () => setStore("state", "complete" /* COMPLETE */) | ||
}); | ||
const setVolume = (volume2) => { | ||
setStore("volume", volume2); | ||
_setVolume(volume2); | ||
}; | ||
if (src instanceof Function) { | ||
createEffect(() => { | ||
const newSrc = access(src); | ||
const newSrc = src(); | ||
if (newSrc instanceof HTMLAudioElement) { | ||
setStore("player", () => newSrc); | ||
setStore("player", newSrc); | ||
} else { | ||
store.player[typeof newSrc === "string" ? "src" : "srcObject"] = newSrc; | ||
setAudioSrc(store.player, newSrc); | ||
} | ||
@@ -130,0 +135,0 @@ seek(0); |
{ | ||
"name": "@solid-primitives/audio", | ||
"version": "1.3.17", | ||
"version": "1.3.18", | ||
"description": "Primitives to manage audio and single sounds.", | ||
@@ -49,8 +49,7 @@ "author": "David Di Biase <dave.dibiase@gmail.com>", | ||
"devDependencies": { | ||
"solid-heroicons": "^3.2.4", | ||
"solid-js": "^1.8.7" | ||
}, | ||
"dependencies": { | ||
"@solid-primitives/static-store": "^0.0.8", | ||
"@solid-primitives/utils": "^6.2.3" | ||
"@solid-primitives/utils": "^6.2.3", | ||
"@solid-primitives/static-store": "^0.0.8" | ||
}, | ||
@@ -57,0 +56,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
21140
7
368