Reactjs 使用nextjs时未定义音频

Reactjs 使用nextjs时未定义音频,reactjs,next.js,Reactjs,Next.js,使用此代码会不断给我错误参考错误:未定义音频。这是因为nextjs是ssr,但应该有一种方法使其工作 const musicPlayers = useRef<HTMLAudioElement>( new Audio("") ); 替换: const musicPlayers=useRef( 音频类型!=“未定义”?新音频(“”):未定义 ); 然后你可以打电话: musicPlayers.current?.play(); MusicLayers

使用此代码会不断给我错误
参考错误:未定义音频
。这是因为nextjs是ssr,但应该有一种方法使其工作

  const musicPlayers = useRef<HTMLAudioElement>(
    new Audio("")
  );
替换:

const musicPlayers=useRef(
音频类型!=“未定义”?新音频(“”):未定义
);
然后你可以打电话:

musicPlayers.current?.play();
MusicLayers.current?.pause();

thx,工作起来很有魅力
  const musicPlayers = useRef<false |HTMLAudioElement>(
    typeof Audio !== "undefined" && new Audio("")
  );
Property 'paused' does not exist on type 'false | HTMLAudioElement'
Property 'play' does not exist on type 'false | HTMLAudioElement'.
     if (musicPlayers.current?.paused) {
          musicPlayers.current.play();
        } else {
          musicPlayers.current?.pause();
        }