showing results for - "play audio in react"
Axel
13 Sep 2019
1import React, { useState, useEffect } from "react";
2
3const useAudio = url => {
4  const [audio] = useState(new Audio(url));
5  const [playing, setPlaying] = useState(false);
6
7  const toggle = () => setPlaying(!playing);
8
9  useEffect(() => {
10      playing ? audio.play() : audio.pause();
11    },
12    [playing]
13  );
14
15  useEffect(() => {
16    audio.addEventListener('ended', () => setPlaying(false));
17    return () => {
18      audio.removeEventListener('ended', () => setPlaying(false));
19    };
20  }, []);
21
22  return [playing, toggle];
23};
24
25const Player = ({ url }) => {
26  const [playing, toggle] = useAudio(url);
27
28  return (
29    <div>
30      <button onClick={toggle}>{playing ? "Pause" : "Play"}</button>
31    </div>
32  );
33};
34
35export default Player;
36
Troy
18 Nov 2017
1import React, { useState, useEffect } from "react";
2
3const useAudio = url => {
4  const [audio] = useState(new Audio(url));
5  const [playing, setPlaying] = useState(false);
6
7  const toggle = () => setPlaying(!playing);
8
9  useEffect(() => {
10      playing ? audio.play() : audio.pause();
11    },
12    [playing]
13  );
14
15  useEffect(() => {
16    audio.addEventListener('ended', () => setPlaying(false));
17    return () => {
18      audio.removeEventListener('ended', () => setPlaying(false));
19    };
20  }, []);
21
22  return [playing, toggle];
23};
24
25const Player = ({ url }) => {
26  const [playing, toggle] = useAudio(url);
27
28  return (
29    <div>
30      <button onClick={toggle}>{playing ? "Pause" : "Play"}</button>
31    </div>
32  );
33};
34
35export default Player;
Dot
10 Aug 2018
1import ReactAudioPlayer from 'react-audio-player';
2//...
3<ReactAudioPlayer
4  src="my_audio_file.ogg"
5  autoPlay
6  controls
7/>
8
Amir
08 Jun 2019
1class Music extends React.Component {
2  state = {
3    play: false
4  }
5  audio = new Audio(this.props.url)
6
7  componentDidMount() {
8    audio.addEventListener('ended', () => this.setState({ play: false }));
9  }
10
11  componentWillUnmount() {
12    audio.removeEventListener('ended', () => this.setState({ play: false }));  
13  }
14
15  togglePlay = () => {
16    this.setState({ play: !this.state.play }, () => {
17      this.state.play ? this.audio.play() : this.audio.pause();
18    });
19  }
20
21  render() {
22    return (
23      <div>
24        <button onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button>
25      </div>
26    );
27  }
28}
29
30export default Music;
Diarmuid
27 Jun 2019
1import React from 'react';
2import ReactDOM from 'react-dom';
3import ReactTestUtils from 'react-dom/test-utils';
4import ReactAudioPlayer from '../src/index.tsx';
5describe('ReactAudioPlayer', function() {
6  const song = './fixtures/turkish_march.ogg';
7  test('renders an audio element', function() {
8    const instance = ReactTestUtils.renderIntoDocument(
9      <ReactAudioPlayer />
10    );
11    const instanceEl = ReactDOM.findDOMNode(instance);
12    expect(instanceEl.tagName).toBe('AUDIO');
13  });
14  test('sets the loop attribute if provided', function() {
15    const instance = ReactTestUtils.renderIntoDocument(
16      <ReactAudioPlayer
17        src={song}
18        loop
19      />
20    );
21    const instanceEl = ReactDOM.findDOMNode(instance);
22    expect(instanceEl.getAttribute('loop')).not.toBe(null);
23  })
24  test('sets title', function() {
25    const instance = ReactTestUtils.renderIntoDocument(
26      <ReactAudioPlayer
27        src={song}
28        title="Turkish march"
29      />
30    );
31    const instanceEl = ReactDOM.findDOMNode(instance);
32    expect(instanceEl.getAttribute("title")).toBe("Turkish march");
33  })
34  test('receives all custom props', function() {
35    const instance = ReactTestUtils.renderIntoDocument(
36      <ReactAudioPlayer
37        src={song}
38        name="custom-name"
39        data-id="custom-data"
40        controlsList="nodownload"
41      />
42    );
43    const props = Object.keys(instance.props);
44    expect(props).toContain('name');
45    expect(props).toContain('data-id');
46    expect(props).toContain('controlsList');
47  });
48});
Joaquín
19 Mar 2019
1npm install --save react-audio-player
2
queries leading to this page
react audio video playerplay audio button reactreact online audio playercreate audio player in react 3fnpm react aduio player librariesaudio player reactaudio player reactjshow to play audio reactjshow put sound on react buttonaudio react propsreactaudioplayer functionhow to play audio in react on load of a componentaudio tag reactaudio 28 29reacttrigger audio reactpause audio from page reactjshow play audio in reactreact sound player ur 3bautoplay audio reactjsreact functional component audio playerplay sound in react jsjsx audio buttonreact audio soundaudio player npm reactusing audio in reactjsplay sound when click image reactplay sound in react apphow to play audio on reactaudio 28 reactaudio play button reactplaying audio reacthow to play a sound in react jsplay sound react componentreact music playeraudio react jsaudio reactjsmusic player reactnpm react audio playerhow to play sound in reactjsplay audio reactreact audio player npmreact audio player editornew audio in reactreact audio loadaudio in jsxaudio player using reactplay sounds with reactput sound in reactreact play audio from urlvideo 2faudio player reactreact audio player demoplay music with react nativereact audio player autoplayhow to play a song throughout the whole application 2breactreact make soundthis audio play reactaudio player in reactjsreact how to play a soundplay audio sound in react jsaudio onclick pause music react typescriptvideo player with audio tracks reactreactaudioplayer npm react nativesound music player reactplay mp3 in react new audio 28 29audio file on react jsreact play sound 3caudio 3e in reactaudio attributes react react new sound objectreact audio player with playback react player audioplaying audio in reacthtml audio in reactreact player for audioaudio play reactjsuse audios in reactcreate audio player reacthow to make a react page play a soundreact play song in background play audio for react jsreact audio player from linkhow to use html audio in react jsreact audio htmlplayer react audiohow to play audio on button click in reactplay sound in javascriptplay sounds react appreact play audio filereact play sound in backgroundreact native audio player componentauto play audio in reactreact play sound onclickhow to create video player with audio in react jsreact sound local state playhow to play and stop the audio tag in react jsplaysound react jsreact sound 2cplay and sound pause codehow to use audio tag in reactadd audio reactmusic player in reactadd audio sound reactjshow to set audio play sound level in reactreact onload musichow to play audio in reactreact html5 audio playerhow to play a sound in reactjshow to play audio from server reactjsnew audio react classreact use audio playerupload audio and play reactreact play sound actionsound in react jshow to play audio on page load reactaudio play 28 29 reactauido playing in reactract js audioreact audio playerhow stream audio player reactreact muusic palyerreact use js audio play audio from urlreact audio player notationreactjs audio player examplehow to add audio to reactjscreating a audio player in reactreactjs play audioadd audio and video in react jsplay sound react soundrecorder audio reactjshow to play sound in react jsdownload sound from api reactaudio player in reactreact audio player componentreact play soundsreact play sound functionminimal react audio playerreact play sound on eventreact download audio filereact audio player htmlaudio element player reactmake a react audio seekbar sound play and sound pause code reactreact audio play buttonsreact wave audio playeraudio player components reactreact audio player with ookshow to make audio player in reactsound react play on startplay mp3 in reactreact audio player from filereact audio playreact button play soundplay audio using button handler and audio element reactreact play sound on functionimporting fcmusic to reacthow to put sound on react appreact audio player customaudio seekbar in reactusing audio in reactaudio play in reactjshow to play object audio raectreact js play audiohow to use javascript play 28 29 in react classreact play pause sound filereact audio playterhow to trigger 3caudio 3e in reactreact play sound on clickaudio player library in reactreact js play audio fileaudio in reactjsplay audio web app reactplay sound javascript reacyaudioplayer in reactjavascript react to soundload sound file in reactaudio element reactaudio events javascript reactaudio media player using reactmusic player with reactplay audio in raectaudio player library reactreact audioplayerplay mp3 on axios return reactjsreact on play audioreact audio player component donereact how to play audioreact play sound when component mountedplay audi reactreact onload audioreact audio player uiaudio object reactaudio player for reactjsreact play local audioplay audio javascript in reacthow to input audio into react pagehow to add audio on reactaudio play reactreactjs audio player npmhow to add audio in reactplay music react jsreact html audio playerplay sound effect in react jshow to give sound access to a website in reactjsplaying sound in react jshow to play audios in reacthow to play a sound reactplay audio from server react jsreact play sounddreact audio jukeboxaudioplayer reactwhen order come start playing sound in react jsreact event play soundusing react audio playerhow to play sound on reactreact simple audio playerplay a sound in reactreact audi playerreact audio player automaticreactjs audio playerhow to play audio on page load in reactplay audio in recat jshow to upload music to use with react audio playerreact get playback audioreact add a sound to a reactjsx audio controlsreact sound play on clickplay sound file in reacthow play audio files on react 21npm audio seekbarreact sound playeraudio player react jsplay auto audio in react 3freact player mp3play sound in reactjsantd audio playerplay music onload reactaudio player react js templatesadding audio to reactplay audio in html5 from request reacthow to click a button then play music in reactplaysoundoncapture react taivereact play audio from backendreact audio play 28 29get song to play in modal reactplay sound reactreact play sound for elementsembed music reactaccess audio time newaudio reactthow to create audio player in reactreact audio playbackplay audio in react js when component loadsadding audio to reactjs appreact audio playplay audio reactjsreact drag and drop audio player npmreact audio elementplay audio javascript reactreact play sound filereactjs play audio filereact audioget noise to play when button is pressed in reactplay audio component reactexample of react audio playerhow run audio playlist reactsimple audio play in react jsaudio player react libraryreact audio stateplay audio no reactreact js audio controlshow to play mp3 sound in reactreact audio player npmload audio in reactreact play audio on clickreactjs play a soundreact play music using urlhow to add audio to reactreact js this audio playaudio playing button reacthow to play a sound in javascript reactaudio player react looks goodreact js music playerwelcome sound after componentdidmount in reactjsplay audio on reactload sound react jshow to do audio record in reactjshow to play audio file in reactreact audio componentplay audio in reactplay audio on button reactusesound hookreact js live audio playerreact play audio repatedlyget playing audio signal from reactreact audio seekbarreact play audio programatticallyaudio players reacthow to play sound in reactreact play sound on mountaudio player in react jshow to use audio file in reactaudio players in react jsadd sound effect in reactjsreact js audioplay audio from url reactmusic player react jsaudio tag html reactplay an audio in reacthow to call component and play audio inside it reactjsaudio with reactjsreact js audio player with imagepause audio from page reactjs functionplayer audio reactjsimplement the audio player in the reactjs mediumnew audio reactauto play audio in html react jsreact mp3 playerhow to play audio on computer reacthow to use a react audio player packageplay an audio when click image reactplay audio corresponding to list item reactreactjs music playeraudio in reactautomatic play sound in react jsaudio tag react jsreact js add audioract audio playerhow to play audio from a link reactreact audio player playlisthow play audio in react jshow to use audio in react jsimplement audio player in react mediumaudio on load reactplay audio react jsreact audio player libraryreact js play soundreactjs audioreact play audio on loadhow to give sound access to a website in react jsaudio player wave reactreact audio playerreact audio player play on an actionthis audio play 28 29 in react componenthow to play audio reactaudio onclick not working react typescriptreact library for audio playimport audio in reacthow to add audio player reactaudio player in react appreact switch by controlling soundaudio in react javascriptreact js play sound on page openaudio in react jsaudio play reactplay sound when button clicked in react componentaudio player with react jsplay audio with react jsload audio reactplay sound from mp3 in reacthtml audio player code for reacthandle audio pause on typescript reactreact play audio on mobileplay a sound reactadd audio to reactplay sound in react app using urlhtml audio player in react js react web audio playerhow to play audio in reactjsaudio reacis it possible to store audio files in state variable reactjsreact audio player cardreact audio recorderaudio player with react 3caudio 3e reactjsplay sound on click reacthow to import sound in reactplay sound onload reactsimple audio player reactreact audio player simplereact audio playlist playerplay song reacthow to play voice in reactplay audio in browser reacton clicking button play welcome sound in react react mobile audio playeradd sound to react appreact 3caudioplayerhow to play audio from event listener reactaudio component reactjshow to impor audio and play in reactplay a sound using reactreact new audio play 28 29play sound on react apphow to use audio player in reacthtml play sound reactreact js audio playermake button play audio in reactaudio player componetnt reactreact hooks audio pauseuse sound react play sound when screen loadsreact audio player srcplay audio in react jsaudio play in react jsaudio methods reactreact audio player onclick playplay audio onclick reactplay sound in react using usesound onloadplay sound react jsrestyle react audio playerimport sound file reacthow to pass audio to audio player reactadd audioplay to my music in reactcreate audio player in reactaudio onclick pause music not working react typescriptplaying audio file in reacthow to record audio in reactreact audio player npmreact audio player exampleplay audio in div reacthow to add a reactjs audio player to websitereact voice playeraudio html react audio player in react js mediumreact use audio player from apireact audio player styleplay sound when component mount in react jsreact play sound on state changereact audio player html cssadding audio to react site 3astackoverflow complay sound onclick reactlecteur music reactjsreproduce audio reactaudio component reactreact play a soundreact play soundwsnew audio react jsplayer audio reactreactaudioplayer function componentreactaudioplayer npmhow to make audio play on button click reactplay sound in react eventaudio plays for a second and stops reactjshow to play audio element from file in component reactjsreact auudiio pllayerhow to play sound file in reactbuild an audio play with reactreact audio in reactjs react sound player player buttonuse sound reactreact audio player samplesvolume audio reactjsreactjs sound effectsimplement audio player in reactreact beep soundsplay audio on click reacthow to play audio and controls in reactplay audio samples in reacthow to add audio player in reactadd audio in react jsexecute a sound reactreact audio player tutorialreact speak audio file mp3audio player in react npmaudio reactreact play audio from filereact press buton to play audioreact play sound onclick examplereact new audioclick play sound reactreact audio player exaplesreact js audioshow to import sound with reactplay sound javascript reactaudio autoplay reactreact play sounds locallyreact audio player html csplay sound with reacthtml audio player react jsget audio to play reactreact check audio is completedreact soundreact js for audio playerhow play sound in reactreact sound player work on click but not automaticallyreact play sound on component renderhow to use an audio player package in reactjshow to play music with reactjshow to play sound in jsxuse audio reactausio in reactreact video playerfunction react play sound effectreact js listen to audio player play a song in reactaudio player reacytreact audio component downloadimplement audio player in react js mediummake an audio player with reactaudio element in reactplay audio on react component loadreact audio videohow to play a sound on an event reactreact playerplay audio in reacct jsreact audio player from apihow to test a sound is playing in react 3fhow to implement audio player in reactplay music in click reathis audio reactplay sound in reactreact audio tagplay a sound in react jsadd audio to eve3ry click in reactreact sound filereact player sound fileplay sound on page load react jsreact play audio serverplay sound api reactreact sound effectsplaying audio on reactreact audio packagereact load audionpm react audiowave audio player reactsound on click reacthow to make button play song reactreact play audiois it possible to store audio files in state variables reactjsplay sounds in reactaudio reactaudio javascript in react componentreact audio preact play mp3 audioreact play sound on call receivehtml5 audio in reactaudio react jshow to play audio with reactplay sound in react js examplereact how to run audioaudio tag in reactreact audio player demohow to play audio in react jsreactjs play soundreact video player with audiosound player reactreact js import mp3 in audio streamreact audio player with controlsreact how to play audio from a filehow to use audio in reactreact audio stream playerplaying audio via reactjsconditionally play audio reactjsplay javascript audio in reactcreate audio player in react jsreact player audiofree audio music import to reactsimple react audio playerplay audio with reactreact play audio on button clickaudio play 28 29 in reacthtml5 audio player reactaudio react playerreact record audioreact sound playmodal play audio reactplay audio in react