-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running into a build failure trying to create a 'new Audio()'...thingy? #19522
Comments
new Audio()
...thingy?
Audio is not supported while we generate html files. Please checkout https://www.gatsbyjs.org/docs/using-client-side-only-packages/ We're marking this issue as answered and closing it for now but please feel free to reopen this and comment if you would like to continue this discussion. We hope we managed to help and thank you for using Gatsby! 💜 |
hmmmm...I looked at that document but I don't really follow what the best approach for me would be. I have this inside an import React, { useState, useMemo, useContext, useEffect } from 'react'
const MusicPlayerContext = React.createContext([{}, () => {}])
const MusicPlayerProvider = props => {
// a bunch of other code...
const [state, setState] = useState({
audioPlayer: new Audio(),
currentTrackIndex: null,
isPlaying: false,
})
// a bunch of other code...
return (
<MusicPlayerContext.Provider
value={{
playTrack,
playArticleTrack,
togglePlay,
currentTrackName,
currentTrackArtist,
currentTrackArtwork,
currentTrackAudio,
currentTime,
setCurrentTime,
formattedDuration,
formattedTime,
audioPlayer: state.audioPlayer,
trackList,
isPlaying: state.isPlaying,
playPreviousTrack,
playNextTrack,
}}
>
{props.children}
</MusicPlayerContext.Provider>
)
}
// access global state from MusicPlayerContext
function useMusicPlayerState() {
return useContext(MusicPlayerContext)
}
export { useMusicPlayerState, MusicPlayerProvider } ...so I guess is there a simpler way to make this work somehow? I'm using React Context to create the audio context for all music to flow through, so it seems odd to try and use any of the solutions provided in the linked document, no? I don't really have a clue as to how I would go about doing any of those other things as well, to be honest. |
@rchrdnsh I'm going through the same problem now. My solution is to use for example: const audioRef = useRef({ current: null });
// on return
<audio ref={audioRef} /> |
hmmmm...could I use a ref if it's outside of the |
I thought of adding a global audio tag and store the ref in context. |
I don't know if that would work either, as I'm calling So stumped by this... Do you have any thoughts on this @KyleAMathews ? Maybe an Async |
Try to install |
hmmm...looking at the documentation for both |
so, @universse came up with a simple and awesome solution for my issue, which was to declare the const [state, setState] = useState({
audioPlayer: {},
currentTrackIndex: null,
isPlaying: false,
})
const [currentTime, setCurrentTime] = useState(state.audioPlayer.currentTime)
useEffect(() => {
setState({
audioPlayer: new Audio(),
currentTrackIndex: null,
isPlaying: false,
})
}, []) ...and it builds now! XD |
Thanks @rchrdnsh it's totally work. |
You are a genius, thanks I adapted that to my code. The secret is new Audio inside useEffect |
export default function Player({ url, className }) {
const [playing, setPlaying] = useState(false);
const [audio] = useState(typeof Audio !== "undefined" && new Audio(url));
useEffect(() => {
playing ? audio.play() : audio.pause()
}, [playing])
// your codes
} I refer this code |
THANK YOU @rchrdnsh you and @universse are literally lifesavers this issue was keeping me up at night |
So, I have an app I'm working on that has a persistent music player built into it using context and hooks, etc. I runs fine in development, but when I try to build it, I get the following error in the terminal:
I went to the debug-html page, but I don't really understand what's going on or how to fix it. The article talks about checking if window is defined, but I tried doing that here and got a bunch of linting errors and other issues by trying to wrap the
new Audio()
thingy in an if statement...I guess that's not ok?So, yeah, don't really know what to do or how to fix the issue :-(
The text was updated successfully, but these errors were encountered: