Skip to content
Advertisement

Playing sound in React.js

JavaScript

This is the code that I am using to play the sound with url (this.url) in my react app. When I press the play button, it gives me an error

Uncaught TypeError: Cannot read property ‘setState’ of undefined

I am not sure why this is happpening since I don’t see any undefined states. A;; states have been declared.

I am new to react so I might be missing something very important.

Please help!

Advertisement

Answer

ES6 class properties syntax

JavaScript

Hooks version (React 16.8+):

JavaScript

Update 03/16/2020: Multiple concurrent players

In response to @Cold_Class’s comment:

Unfortunately if I use multiple of these components the music from the other components doesn’t stop playing whenever I start another component playing – any suggestions on an easy solution for this problem?

Unfortunately, there is no straightforward solution using the exact codebase we used to implement a single Player component. The reason is that you somehow have to hoist up single player states to a MultiPlayer parent component in order for the toggle function to be able to pause other Players than the one you directly interacted with.

One solution is to modify the hook itself to manage multiple audio sources concurrently. Here is an example implementation:

JavaScript

Example App.js using the MultiPlayer component:

JavaScript

The idea is to manage 2 parallel arrays:

  • your audio sources (built from the urls props you pass to the parent component ; the urls props is an array of strings (your MP3 URLs))
  • an array tracking the state of each player

The toggle method updates the player state array based on the following logic:

  • if there is a player currently active (i.e. audio is playing) and this active player is not the player targeted by the toggle method, revert that player’s playing state to false, and set the targeted player’s playing state to true [you clicked on ‘play’ while another audio stream was already playing]
  • if the player currently active is the player targeted by the toggle method, simply revert the targeted player’s playing state to false [you clicked on ‘pause’]
  • if there is no player currently active, simply set the targeted player’s state to true [you clicked on ‘play’ while no audio stream was currently playing]

Note that the toggle method is curried to accept the source player’s index (i.e. the index of the child component where the corresponding button was clicked).

Actual audio object control happens in useEffect as in the original hook, but is slightly more complex as we have to iterate through the entire array of audio objects with every update.

Similarly, event listeners for audio stream ‘ended’ events are handled in a second useEffect as in the original hook, but updated to deal with an array of audio objects rather than a single such object.

Finally, the new hook is called from the parent MultiPlayer component (holding multiple players), which then maps to individual Players using (a) an object that contains the player’s current state and its source streaming URL and (b) the toggle method curried with the player’s index.

CodeSandbox demo

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement