-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
13 changed files
with
121 additions
and
92 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
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,18 +1,17 @@ | ||
import {useAtom} from 'jotai'; | ||
import {useEffect} from 'react'; | ||
import {isRepeatingAtom} from 'src/atoms/repeat.atoms'; | ||
import {useAudioRefContext} from 'src/contexts/audio-ref-context'; | ||
|
||
/** | ||
* Hook to set the loop on audio element | ||
*/ | ||
export function useAudioLoop(audio: HTMLAudioElement | null): void { | ||
export function useAudioLoop() { | ||
const ref = useAudioRefContext(); | ||
const [isRepeating] = useAtom(isRepeatingAtom); | ||
|
||
useEffect(() => { | ||
if (!(audio instanceof HTMLAudioElement)) { | ||
if (ref.current === null) { | ||
return; | ||
} | ||
|
||
audio.loop = isRepeating; | ||
}, [audio, isRepeating]); | ||
ref.current.loop = isRepeating; | ||
}, [ref, isRepeating]); | ||
} |
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,25 +1,28 @@ | ||
import {useAtom} from 'jotai'; | ||
import {useEffect} from 'react'; | ||
import {isPlayingAtom} from 'src/atoms/play-pause.atoms'; | ||
import {useAudioRefContext} from 'src/contexts/audio-ref-context'; | ||
|
||
/** | ||
* Hook to use audio play/pause | ||
*/ | ||
export function useAudioPlayPause(audio: HTMLAudioElement | null): void { | ||
export function useAudioPlayPause() { | ||
const ref = useAudioRefContext(); | ||
const [isPlaying] = useAtom(isPlayingAtom); | ||
|
||
useEffect(() => { | ||
if (!(audio instanceof HTMLAudioElement)) { | ||
if (ref.current === null) { | ||
return; | ||
} | ||
|
||
(async () => { | ||
const audio = ref.current; | ||
|
||
const toggle = async () => { | ||
if (isPlaying) { | ||
await audio.play(); | ||
return; | ||
} | ||
|
||
audio.pause(); | ||
})(); | ||
}, [isPlaying, audio]); | ||
}; | ||
|
||
toggle().then(); | ||
}, [ref, isPlaying]); | ||
} |
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,18 +1,17 @@ | ||
import {useAtom} from 'jotai'; | ||
import {useEffect} from 'react'; | ||
import {speedAtom} from 'src/atoms/speed.atoms'; | ||
import {useAudioRefContext} from 'src/contexts/audio-ref-context'; | ||
|
||
/** | ||
* Hook to set audio playback rate | ||
*/ | ||
export function useAudioPlaybackRate(audio: HTMLAudioElement | null): void { | ||
export function useAudioPlaybackRate() { | ||
const ref = useAudioRefContext(); | ||
const [speed] = useAtom(speedAtom); | ||
|
||
useEffect(() => { | ||
if (!(audio instanceof HTMLAudioElement)) { | ||
if (ref.current === null) { | ||
return; | ||
} | ||
|
||
audio.playbackRate = speed; | ||
}, [audio, speed]); | ||
ref.current.playbackRate = speed; | ||
}, [ref, speed]); | ||
} |
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,18 +1,17 @@ | ||
import {useAtom} from 'jotai'; | ||
import {useEffect} from 'react'; | ||
import {seekAtom} from 'src/atoms/seek.atoms'; | ||
import {useAudioRefContext} from 'src/contexts/audio-ref-context'; | ||
|
||
/** | ||
* Hook to set audio player's current time | ||
*/ | ||
export function useAudioSeek(audio: HTMLAudioElement | null): void { | ||
export function useAudioSeek() { | ||
const ref = useAudioRefContext(); | ||
const [seek] = useAtom(seekAtom); | ||
|
||
useEffect(() => { | ||
if (!(audio instanceof HTMLAudioElement)) { | ||
if (ref.current === null) { | ||
return; | ||
} | ||
|
||
audio.currentTime = seek; | ||
}, [audio, seek]); | ||
ref.current.currentTime = seek; | ||
}, [ref, seek]); | ||
} |
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,25 +1,28 @@ | ||
import {useAtom} from 'jotai'; | ||
import {useEffect} from 'react'; | ||
import {setVolumeAtom, volumeAtom} from 'src/atoms/volume.atoms'; | ||
import {useAudioRefContext} from 'src/contexts/audio-ref-context'; | ||
|
||
/** | ||
* Hook to set audio volume | ||
*/ | ||
export function useAudioVolume(audio: HTMLAudioElement | null): void { | ||
export function useAudioVolume() { | ||
const ref = useAudioRefContext(); | ||
const [volume] = useAtom(volumeAtom); | ||
const [, setVolume] = useAtom(setVolumeAtom); | ||
|
||
useEffect(() => { | ||
if (!(audio instanceof HTMLAudioElement)) { | ||
if (ref.current === null) { | ||
return; | ||
} | ||
|
||
const audio = ref.current; | ||
|
||
audio.volume = volume; | ||
|
||
const handleVolumeChange = () => setVolume(audio.volume); | ||
|
||
audio.addEventListener('volumechange', handleVolumeChange); | ||
|
||
return () => audio.removeEventListener('volumechange', handleVolumeChange); | ||
}, [audio, setVolume, volume]); | ||
return () => { | ||
audio.removeEventListener('volumechange', handleVolumeChange); | ||
}; | ||
}, [ref, setVolume, volume]); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { | ||
createContext, | ||
type MutableRefObject, | ||
useContext, | ||
useRef, | ||
} from 'react'; | ||
|
||
type Audio = HTMLAudioElement | null; | ||
|
||
const AudioRefContext = createContext<MutableRefObject<Audio>>( | ||
{} as MutableRefObject<Audio>, | ||
); | ||
|
||
export function AudioRefContextProvider({children}) { | ||
const audioRef = useRef<Audio>(null); | ||
|
||
return ( | ||
<AudioRefContext.Provider value={audioRef}> | ||
{children} | ||
</AudioRefContext.Provider> | ||
); | ||
} | ||
|
||
export function useAudioRefContext() { | ||
return useContext(AudioRefContext); | ||
} |
Oops, something went wrong.