audio in react

import React, { useState, useEffect } from "react";

const useAudio = url => {
  const [audio] = useState(new Audio(url));
  const [playing, setPlaying] = useState(false);

  const toggle = () => setPlaying(!playing);

  useEffect(() => {
      playing ? audio.play() : audio.pause();
    },
    [playing]
  );

  useEffect(() => {
    audio.addEventListener('ended', () => setPlaying(false));
    return () => {
      audio.removeEventListener('ended', () => setPlaying(false));
    };
  }, []);

  return [playing, toggle];
};

const Player = ({ url }) => {
  const [playing, toggle] = useAudio(url);

  return (
    <div>
      <button onClick={toggle}>{playing ? "Pause" : "Play"}</button>
    </div>
  );
};

export default Player;

3
1
Krish 100200 points

                                    import ReactAudioPlayer from 'react-audio-player';
//...
&lt;ReactAudioPlayer
  src=&quot;my_audio_file.ogg&quot;
  autoPlay
  controls
/&gt;

3 (1 Votes)
0
4.33
6
Awgiedawgie 440220 points

                                    import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import ReactAudioPlayer from '../src/index.tsx';
describe('ReactAudioPlayer', function() {
  const song = './fixtures/turkish_march.ogg';
  test('renders an audio element', function() {
    const instance = ReactTestUtils.renderIntoDocument(
      &lt;ReactAudioPlayer /&gt;
    );
    const instanceEl = ReactDOM.findDOMNode(instance);
    expect(instanceEl.tagName).toBe('AUDIO');
  });
  test('sets the loop attribute if provided', function() {
    const instance = ReactTestUtils.renderIntoDocument(
      &lt;ReactAudioPlayer
        src={song}
        loop
      /&gt;
    );
    const instanceEl = ReactDOM.findDOMNode(instance);
    expect(instanceEl.getAttribute('loop')).not.toBe(null);
  })
  test('sets title', function() {
    const instance = ReactTestUtils.renderIntoDocument(
      &lt;ReactAudioPlayer
        src={song}
        title=&quot;Turkish march&quot;
      /&gt;
    );
    const instanceEl = ReactDOM.findDOMNode(instance);
    expect(instanceEl.getAttribute(&quot;title&quot;)).toBe(&quot;Turkish march&quot;);
  })
  test('receives all custom props', function() {
    const instance = ReactTestUtils.renderIntoDocument(
      &lt;ReactAudioPlayer
        src={song}
        name=&quot;custom-name&quot;
        data-id=&quot;custom-data&quot;
        controlsList=&quot;nodownload&quot;
      /&gt;
    );
    const props = Object.keys(instance.props);
    expect(props).toContain('name');
    expect(props).toContain('data-id');
    expect(props).toContain('controlsList');
  });
});

4.33 (6 Votes)
0
0
0
Awgiedawgie 440220 points

                                    class Music extends React.Component {
  state = {
    play: false
  }
  audio = new Audio(this.props.url)

  componentDidMount() {
    audio.addEventListener('ended', () =&gt; this.setState({ play: false }));
  }

  componentWillUnmount() {
    audio.removeEventListener('ended', () =&gt; this.setState({ play: false }));  
  }

  togglePlay = () =&gt; {
    this.setState({ play: !this.state.play }, () =&gt; {
      this.state.play ? this.audio.play() : this.audio.pause();
    });
  }

  render() {
    return (
      &lt;div&gt;
        &lt;button onClick={this.togglePlay}&gt;{this.state.play ? 'Pause' : 'Play'}&lt;/button&gt;
      &lt;/div&gt;
    );
  }
}

export default Music;

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
react play sounds locally play audio sound in react js make an audio player with react play audio with react audio element in react react &lt;AudioPlayer audio player in react js new audio react react-use js Audio play audio from url audio react props video player with audio tracks react add audio and video in react js how play audio in react react js audios how to use html audio in react js html audio in react react new audio react play local audio import sound file react audio tag react js add audio sound reactjs audio tag react audio player components react audio component reactjs audio player library in react auto play audio in react import audio in react react audio video player create audio player in react? react play mp3 audio react-player mp3 audio player using react react audio stream player execute a sound react play sound when component mount in react js react audio player automatic react play sound on function react play sound on event play audio in react js react play sound on component render audio player react library react audio player from link audio player componetnt react audio player reacyt play audio samples in react play sound in react audio player with react react mp3 player creating a audio player in react how to play audio and controls in react music player in react how to play audio reactjs react audio player editor react audio player react simple audio player play sounds in react react-player for audio sound music player react sound player react play audio in reacct js audio reactjs react audio player tutorial how to play audio on react play audio on click react how to play audio on page load in react react play pause sound file react button play sound react play sound file reactjs audio player example react js add audio play sound on page load react js react online audio player react web audio player how to use audio file in react audio react player react html5 audio player play a sound in react audio play in reactJs how to play a sound react audio with reactjs REACT audio player from file play sound onload react play audio for react js play sound in react js how to create video player with audio in react js react video player with audio react js this.audio.play play sound effect in react js play audio on button react react audio player playlist react audio player samples audio play in react js get audio to play react react audio p react-sound play on click how play sound in react how to play a sound in javascript react react audio package play sound onclick react use-sound react play sound when screen loads how to play audio file in react play sound in reactjs react js play sound play sound on click react react play audio on click play song react load sound file in react play sound file in react react load audio html5 audio player react react-audio-player onClick play automatic play sound in react js play audio react js react audio player style Simple react audio player audio player react looks good react play audio on mobile reactjs audio react-audio-player demo react js listen to audio player how to add audio player react react audio player html react how to play a sound react play sound onclick example using react-audio-player react audio video react play sound onclick new audio react js react audio player component done react audio player demo how stream audio player react playing audio react how to add audio to reactJS how to play audio on button click in react playing audio on react put sound in react react record audio this.audio.play react how to play a sound in react.js html audio player react js react play sounds play audio on react component load react audio player simple how to play audio on computer react how to play audio with react html play sound react react play audio from backend play sound on react app how to add audio in react play sound with react react audio.play react how to play audio from a file react how to play audio react js audio player with image react audio player with controls play audio in react audio.play() react audio element player react play audio in browser react how to play audio in react js how to play sound in react.js how to play sound on react how to impor audio and play in react play a sound react create audio player in react react audio player card react-player audio play sound javascript react play sound javascript reacy react play sound on call receive build an audio play with react how to create audio player in react react player sound file reactjs play audio file react sound player how to use audio player in react create audio player react react play sound for elements reactjs play audio play an audio when click image react play sound when click image react react audio play react check audio is completed play music react js adding audio to reactjs app audio player react js templates react player audio audio player reactjs react how to run audio react audio html audio html react ract js audio react mobile audio player react event play sound how play audio files on react audio file on react js react play audio server function react play sound effect react play sound on click play audio in html5 from request react react audio jukebox react library for audio play html5 audio in react adding audio to react site:stackoverflow.com adding audio to react react add a sound to a react sound in react js audio autoplay react react audio player library play audio using button handler and audio element react lecteur music reactjs play music with react native how to record audio in react volume audio reactjs audio media player using react play a sound in react js react audio player with ooks recorder audio reactjs react audio recorder audio tag in react audio players in react js play sound in react using usesound onload react audio player autoplay react player download sound from api react play music in click rea play audio onclick react react functional component audio player audio in react js embed music react react sound player ur; React audio component importing FcMusic to react react auudiio pllayer react audioplayer how to play audio react play sounds react app react play audio programattically playsound React JS react download audio file how to play object audio raect Audio react react audio player npm react switch by controlling sound react sound,play and sound.pause code sound.play and sound.pause code React play audio javascript in react play mp3 in react how to play sound in jsx play sound in react app using url play sound in react app how to import sound with react play audio button react audio playing button react npm react aduio player libraries play sound react sound how to play sound in react autoplay audio reactjs &lt;audio&gt; reactjs how to add a reactjs audio player to website how to pass audio to audio player react how to use an audio player package in reactjs how to use a react audio player package reactjs audio player npm how to play audio in reactjs how to play audio in react when order come start playing sound in react js auto play audio in html react js audio.play() in react react js for audio player react audio element react audio.play() this.audio react react play audio file add audio to react react-audio-player - npm react drag and drop audio player npm react play sound when component mounted how to input audio into react page how to give sound access to a website in react js how to give sound access to a website in reactjs react-audio-player npm audio in reactjs using audio in reactjs usesound hook play audio in recat js reactjs music player react voice player pause audio from page reactjs function pause audio from page reactjs react play sound in background audio in react conditionally play audio reactjs reactaudioplayer function component reactaudioplayer function reactjs sound effects react sound effects player audio react react music player react js music player add sound to react app free audio music import to react trigger audio react react play soundd npm react audio player audio player in react how to upload music to use with react audio player how to play music with reactjs react audi player react js live audio player jsx audio controls music player react js how to play voice in react react new Audio play() audio events javascript react react speak audio file mp3 use sound react html audio player code for react how to add audio player in react react sound play audio react js react play audio repatedly get playing audio signal from react AudioPlayer react Audio methods react play a sound using react playSoundOnCapture react taive audio play reactjs audio play react jsx audio button audio in jsx how to play and stop the audio tag in react js react make sound react beep sounds add sound effect in reactjs react audio player play on an action react play sound action ausio in react reactaudioplayer npm react native ReactAudioPlayer npm react play music using url react html audio player antd audio player react audio player example audio player in reactjs html audio player in react js react muusic palyer play a song in react react audio player with playback react get playback audio react audio playback npm react audio audioplayer in react react wave audio player react audio player component audio player react audio javascript in react component react play audio from file welcome sound after componentdidmount in reactjs on clicking button play welcome sound in react simple audio player react access audio time newAudio reactt react sound local state play modal play audio react play audio corresponding to list item react get song to play in modal react react onload audio react onload music how put sound on react button react sound player work on click but not automatically auido playing in react upload audio and play react react js import mp3 in audio stream player react audio WAVE AUDIO PLAYER REACT audio player wave react react audio playter make button play audio in react how to make button play song react audio react !npm audio-seekbar make a react audio seekbar react audio seekbar minimal react audio player audio seekbar in react example of react audio player react audio player ui audio player react js react-audio-player from api react-use-audio-player from api react js audio sound on click react audio react.js how to use javascript .play() in react class add audio react react audio player src react audio state reactjs audio player play music onload react how to click a button then play music in react restyle react audio player is it possible to store audio files in state variable reactjs is it possible to store audio files in state variables reactjs play mp3 on axios return reactjs how to trigger &lt;audio&gt; in react react audio component download react js audio controls &lt;audio&gt; in react react js play audio javascript - react to sound how to add audio to react play audi react audio player for reactjs react hooks audio pause How to test a sound is playing in react? react play song in background audio onclick pause music react typescript audio onclick pause music not working react typescript audio onclick not working react typescript react audio load handle audio pause on typescript react music player react how to play a song throughout the whole application +react play audio no react audio play button react react audio react video player react js audio player get noise to play when button is pressed in react audio plays for a second and stops reactjs playing audio via reactjs play audio reactjs play audio from server react js playing sound in react js react audio player how to play audio from server reactjs reproduce audio react play audio on react react play audio from url play sound in react event play audio javascript react click play sound react sound react play on start add audio in react js playing audio in react play sound react play audio react react play sound on state change react new Sound object add audio to eve3ry click in react react play sound react press buton to play audio play sound when button clicked in react component this.audio.play() in react component new Audio react class react play audio add audioplay to my music in react react-sound
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source