useAudioPlayer icon indicating copy to clipboard operation
useAudioPlayer copied to clipboard

Cannot read property 'player' of null

Open carmandale opened this issue 4 years ago • 9 comments

If you have an HTML element inside a react three fiber drei Html component, it won't "wait" for the audio player component to regiter or mount and I get this error.

Cannot read property 'player' of null

carmandale avatar Sep 06 '21 04:09 carmandale

Even if I do...

const value = useAudioPlayer() const { ready } = value console.log(value) if (ready) { return ( <div className="player"> <3D react scene with HTML element /> </div> ) }

carmandale avatar Sep 06 '21 04:09 carmandale

I have meet the same question. Are you find some way to fix that?

Vang-z avatar Dec 11 '21 09:12 Vang-z

hi @carmandale thank you for opening this. Can you describe more about your issue? What library are you using this with? Perhaps we will need to collaborate on a fix if I am not familiar

E-Kuerschner avatar Dec 16 '21 15:12 E-Kuerschner

@E-Kuerschner I am experiencing the same issue while attempting to load the example hook code in the README w/in storybook or Next.js app.

painedpineapple avatar Mar 02 '22 00:03 painedpineapple

same here with nextjs. looks like its ssr related? I've created a simple codesandbox: https://codesandbox.io/s/react-use-audio-player-issue-71-o42ytf

chwzr avatar Apr 11 '22 10:04 chwzr

I have the same problem on GatsbyJS and the solution was wrap the component inside the AudioPlayerProvider, hope this usefull 👍

argkont avatar Aug 12 '22 17:08 argkont

The Provider has to be named in an upper component. The hook is called bevor the provider is returned, that's why the context (=player) is null.

Make a wrapper-component or use a component above. You can look at the example on this git repository. The author is using two different components. You don't need to provide a value for the Provider.

RunRanger avatar Aug 24 '22 23:08 RunRanger

Tried this it doesn't work, this was only use I could find in examples

    <AudioPlayerProvider>
      <div>
        <button onClick={togglePlayPause}>{playing ? 'Pause' : 'Play'}</button>
      </div>
    </AudioPlayerProvider>

And full code

import React from 'react';
import { useAudioPlayer, AudioPlayerProvider } from 'react-use-audio-player';

const AudioPlayer = ({ file }) => {
  const { togglePlayPause, ready, loading, playing } = useAudioPlayer({
    src: file,
    format: 'mp3',
    autoplay: false,
    onend: () => console.log('sound has ended!'),
  });

  if (!ready && !loading) return <div>No audio to play</div>;
  if (loading) return <div>Loading audio</div>;

  return (
    <AudioPlayerProvider>
      <div>
        <button onClick={togglePlayPause}>{playing ? 'Pause' : 'Play'}</button>
      </div>
    </AudioPlayerProvider>
  );
};

export default AudioPlayer;

same error useAudioPlayer.ts:19 Uncaught TypeError: Cannot read properties of null (reading 'player') at useAudioPlayer (useAudioPlayer.ts:19:1)

JanVeb avatar Sep 13 '22 02:09 JanVeb

Solved it by wrapping AudioPlayer in AudioPlayerProvider in imported file

    <AudioPlayerProvider>
        <AudioPlayer />
      </AudioPlayerProvider>

JanVeb avatar Sep 13 '22 03:09 JanVeb

@JanVeb's solution worked for me - make sure the AudioPlayer is wrapped inside AudioPlayerProvider (doesn't matter how far up your component tree this is - I've wrapped it at app level) - thanks :)

codewithfeeling avatar Jan 23 '23 18:01 codewithfeeling

yep! the solution @JanVeb provided is the way the hook is designed to function. I can try and update the docs to make this more clear

E-Kuerschner avatar Apr 16 '23 01:04 E-Kuerschner