#root
View Compiled
const { useRef } = React;

function SoundPlayer() {
  // On initialise la référence audioRef
  const audioRef = useRef();

  const play = () => {
    // Au clic sur le bouton play, on exécute la fonction `play` de l'élément audio
    audioRef.current.play();
  };
  const pause = () => {
    audioRef.current.pause();
  };

  return (
    <div>
    {/* On recupère la référence de l'élément audio */}
      <audio ref={audioRef} src="https://www.bensound.com/bensound-music/bensound-creativeminds.mp3">
        Your browser does not support the
        <code>audio</code> element.
      </audio>
      <button onClick={play}> Play </button>
      <button onClick={pause}> Pause </button>
    </div>
  );
}

ReactDOM.render(<SoundPlayer/>, document.getElementById("root"));
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.8.7/polyfill.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js
  3. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js