-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from OMGDuke/25/fix-blocking-moondeck-interact
Now using an audio object instead of a dom element, no longer causes the issue that makes moondeck non interactable
- Loading branch information
Showing
6 changed files
with
121 additions
and
79 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useEffect, useMemo, useState } from 'react' | ||
|
||
const useAudioPlayer = ( | ||
audioUrl: string | undefined | ||
): { | ||
play: () => void | ||
pause: () => void | ||
stop: () => void | ||
setVolume: (volume: number) => void | ||
togglePlay: () => void | ||
isPlaying: boolean | ||
isReady: boolean | ||
} => { | ||
const audioPlayer: HTMLAudioElement = useMemo(() => { | ||
return new Audio() | ||
}, []) | ||
|
||
audioPlayer.oncanplay = () => { | ||
setIsReady(true) | ||
} | ||
|
||
const [isPlaying, setIsPlaying] = useState(false) | ||
const [isReady, setIsReady] = useState(false) | ||
|
||
useEffect(() => { | ||
if (audioUrl?.length) { | ||
audioPlayer.src = audioUrl | ||
audioPlayer.loop = true | ||
} | ||
}, [audioUrl]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
unload() | ||
} | ||
}, []) | ||
|
||
function play() { | ||
audioPlayer.play() | ||
setIsPlaying(true) | ||
} | ||
|
||
function pause() { | ||
audioPlayer.pause() | ||
setIsPlaying(false) | ||
} | ||
|
||
function stop() { | ||
audioPlayer.pause() | ||
audioPlayer.currentTime = 0 | ||
setIsPlaying(false) | ||
} | ||
|
||
function togglePlay() { | ||
isPlaying ? stop() : play() | ||
} | ||
|
||
function setVolume(newVolume: number) { | ||
audioPlayer.volume = newVolume | ||
} | ||
|
||
function unload() { | ||
stop() | ||
audioPlayer.src = '' | ||
setIsPlaying(false) | ||
setIsReady(false) | ||
} | ||
|
||
return { | ||
play, | ||
pause, | ||
stop, | ||
setVolume, | ||
togglePlay, | ||
isPlaying, | ||
isReady | ||
} | ||
} | ||
|
||
export default useAudioPlayer |
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