-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37e2576
commit 8308c9e
Showing
4 changed files
with
152 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
import {noSerialize, useSignal, useVisibleTask$, $, type NoSerialize} from "@builder.io/qwik"; | ||
|
||
export const useSound = (sound: string) => { | ||
export const useSound = (url: string) => { | ||
const audioRef = useSignal<NoSerialize<HTMLAudioElement>>(); | ||
const isPlaying = useSignal(false); | ||
const time = useSignal(0); | ||
const duration = useSignal(0); | ||
|
||
useVisibleTask$(async () => { | ||
const audio = new Audio(sound); | ||
const audio = new Audio(url); | ||
audio.addEventListener("play", () => isPlaying.value = true); | ||
audio.addEventListener("ended", () => isPlaying.value = false); | ||
audio.addEventListener("pause", () => isPlaying.value = false); | ||
audio.addEventListener("timeupdate", () => { | ||
time.value = audio.currentTime; | ||
}) | ||
audio.addEventListener("loadedmetadata", () => { | ||
duration.value = audio.duration; | ||
}) | ||
audioRef.value = noSerialize(audio); | ||
}); | ||
|
||
const play = $(() => audioRef.value?.play()); | ||
const stop = $(() => audioRef.value?.pause()); | ||
|
||
return [play]; | ||
const undo = $((offset: number = 10) => { | ||
if (audioRef.value) { | ||
audioRef.value.currentTime -= offset | ||
} | ||
}); | ||
|
||
const redo = $((offset: number = 10) => { | ||
if (audioRef.value) { | ||
audioRef.value.currentTime += offset | ||
} | ||
}); | ||
|
||
const seek = $((newTime: number) => { | ||
if (audioRef.value) { | ||
audioRef.value.currentTime = newTime; | ||
} | ||
}); | ||
|
||
return {play, stop, isPlaying, time, undo, redo, seek, duration}; | ||
} |
Oops, something went wrong.