play sound onload react

Solutions on MaxInterview for play sound onload react by the best coders in the world

showing results for - "play sound onload react"
Cayson
22 Apr 2020
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;
Jerónimo
11 Feb 2016
1import ReactAudioPlayer from 'react-audio-player';
2//...
3<ReactAudioPlayer
4  src="my_audio_file.ogg"
5  autoPlay
6  controls
7/>
8
Jérémie
14 Nov 2020
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;
queries leading to this page
how to play a song throughout the whole application 2breactplay audio in react js when component loadsreact play audio from urlnew audio react classplaysoundoncapture react taiveaudio play button reactreact functional component audio playeraudio react jsaudio player in reactplay audio in reactplay sound in react app using urlplay an audio in reactreact player sound filehow to play audio on page load in reactreact play sound on component renderplay audio with reactjavascript react to soundreact sound player ur 3badd audio to reactreact play sound functionhow to click a button then play music in reactplay sound file in reacthow to play sound in reactjshow to add audio in reactreact how to play audioreact play sounddadd audio and video in react jsplay sound api reactreact audio play 28 29play music onload reactget playing audio signal from reacthow to play a sound in javascript reactreact sound playerreact play soundwsplay audio javascript in reactreact js add audioreact play sound in backgroundplay sound reacthow to play object audio raectreact sound effectstrigger audio reactwelcome sound after componentdidmount in reactjsreact play sound on eventreact audio in reactjs reactjs play audio filehow to give sound access to a website in react jsreact play audiohow to play audios in reactreactjs play audioplay a sound in reactaccess audio time newaudio reactthow to play sound on reacthow to impor audio and play in reacthow to add audio to reactjsreact js play soundreact audio componentplay sound in reactplay audio on reactusesound hookautoplay audio reactjshow to play audio reactaudio plays for a second and stops reactjsreact hooks audio pauseplay mp3 on axios return reactjsreact play sound on mountplay audio no reactreact press buton to play audioaudio play in react jsaudio in jsxreact play audio on button clickplaysound react jshow to add audio on reactreact add a sound to a reactreact play sounds locallyreact play sound actionhtml play sound reactaudio react jssound react play on startreact play sound onclick examplenew audio react jsplay audio in jsximplement audio player in reactreact play sound onclickreact play soundsplay audio reactreact play music using urlput sound in reactreact audio playplay audio web app reactaudio play 28 29 in reactreact play sound on clickadd audioplay to my music in react 3caudio 3e reactjsadd sound to react appplaying audio on reactplay mp3 in reactreact audio stateplay audio in raectuse sound reactreact new audio play 28 29react play song in background reproduce audio reactwhen order come start playing sound in react jscreating a audio player in reactplay audio in browser reacthow to give sound access to a website in reactjsplay sound on click reactembed music reactadd audio sound reactjsauto play audio in reactreact make soundreact js import mp3 in audio streamreact play audio programatticallyreact player audioload sound react jsreact audio playreact play audio on mobilereact audio player onclick playaudio onclick not working react typescripthow to set audio play sound level in reactget song to play in modal reactreact download audio filefree audio music import to reactplay sound on page load react jsplay audio from url reactadding audio to reactreactjs sound effectsplay a sound in react jsplay audio component reacthow to implement audio player in reactplay sound react componentexecute a sound reactreact sound fileaudio on load reactaudio player in reactjshow to use javascript play 28 29 in react classplay audio in div reacthow to use audio player in reacthow to test a sound is playing in react 3fplay audio sound in react jsaudio player using reacthow to add audio to reactsound in react jsaudio onclick pause music not working react typescriptnew audio in reactausio in reacthow to play a sound in react jsreact play audio from filehow to play sound in reactthis audio reacthow to play audio on page load reactplaying audio file in reactreact audio play buttonsclick play sound reactplay sound when click image reactreact play audio on clickhow to play music with reactjsreact use js audio play audio from urladd audio in react jsplay javascript audio in reactaudio onclick pause music react typescriptadd audio to eve3ry click in reactreact sound playreact check audio is completedhow to trigger 3caudio 3e in reactplay audio using button handler and audio element reactreact js play audio filereact on play audiohow to play mp3 sound in reactplay audio reactjsreact play audio repatedlyplay song reactaudio tag in reactmake button play audio in reactreact js this audio playload sound file in reactplay a sound reactplay music in click reahow to play audio from event listener reactreact speak audio file mp3using audio in reactjsreact play audio serverimport sound file reactaudio playing button reactplay audio from server react jsaudio play in reactjsplay sound when button clicked in react componentaudio methods reactplaying sound in react jshow to import sound with reacthow to play audio in reactreact onload audioaudio play reactreact play sound on functionhow to play audio file in reactreact play pause sound filereact get playback audioplay sound javascript reacyreact sound 2cplay and sound pause codeaudio in reactjsaudio file on react jsreact play soundreact play local audioreact how to play audio from a fileaudio with reactjsplaying audio reactreact play sound on call receivereact onload musichow to add audio player reactreact play sound when component mountedaudio html reacthow to play audio in reactjsaudio player in react appadd sound effect in reactjsreact audiohow to play sound in react json clicking button play welcome sound in react how to use audio in reacthow to play audio on reacthow to make audio player in reactpause audio from page reactjsplaying audio via reactjsaudio player with reacthow play sound in reactplay sound onclick reactthis audio play 28 29 in react componentplay sound on react apphow to make button play song reactaudio play reactjsmake an audio player with reactplay mp3 in react new audio 28 29how to play audio in react jsplay sounds with reacthow to play audio from server reactjsreact audio player with ookshow play audio files on reactplay audio react jsplaying audio in reacthow to import sound in reactget noise to play when button is pressed in reactplay audio onclick reactaudio player with react jsusing audio in reactaudio in reactadd audio in reactplay sound in react js examplemusic player reactplay sound in react eventplay audio on react component loadplay sound javascript reactplay audio samples in reactplay sound in reactjsplay audio in recat jsload audio reacthow to play a sound on an event reactimport audio in reactaudio play 28 29 reacthow to play and stop the audio tag in react jsaudio player in react jshow to record audio in reactreact audio player examplefunction react play sound effecthow to input audio into react pagehow to create audio player in reactplay audio corresponding to list item reactaudio javascript in react componenthow to play a sound in reactjsplayer audio reactjshow to play audio on button click in reactload audio in reactimporting fcmusic to reactreact beep soundsconditionally play audio reactjsreact play sound on state changereactjs play soundplay audio javascript reactmodal play audio reactplay audio with react jshow to use audio file in reactplay sound when component mount in react jshow to play audio with reactaudio player reacthow to make a react page play a soundreact play audio fileadding audio to react site 3astackoverflow comreact play audio on loadaudio in react jshow to play sound in jsxdownload sound from api reactis it possible to store audio files in state variables reactjsplay sound react jsreact record audioplay audio for react jsplay sound onload reactplay audio button reactbuild an audio play with reactreact how to run audioplay audio in react jsreact event play soundhow to play audio element from file in component reactjsreact js play audioreact js audioreactjs play a soundhandle audio pause on typescript reactplay audio on button reactplay sounds in reacthow to pass audio to audio player reactreact button play soundpause audio from page reactjs functionhow put sound on react buttonget audio to play reactaudio reacthow to play audio in react on load of a componentuse sound react play sound when screen loadsplay an audio when click image reactplay a song in reactplay sound in javascripthow to play a sound reactreact js play sound on page openplay sounds react appcreate audio player in react 3freact sound local state playplay sound with reacthow to call component and play audio inside it reactjsplay audi reactaudio autoplay reacthow to play voice in reactreact play sound filethis audio play reactreact how to play a soundreact load audioplay a sound using reactreact play a soundsimple audio play in react jssound on click reactreact sound play on clickhow run audio playlist reacthow play audio in reactplay sound react soundreact play sound for elementshow to play audio and controls in react sound play and sound pause code reactreact play audio from backendhow to make audio play on button click reactautomatic play sound in react jsplay music with react nativeplay audio on click reactis it possible to store audio files in state variable reactjsreact sound player work on click but not automaticallyreact soundplay sound from mp3 in reacthow to put sound on react apphow to play audio on computer reacthow to play audio reactjsplay sound effect in react jsjsx audio buttonreact new sound objectplay sound in react jsauto play audio in html react jsplay auto audio in react 3fcreate audio player in react jshow to play sound file in reactplay sound in react apphow to play audio from a link reactreact switch by controlling soundplay audio in reacct jsadd audio reactaudio play react play sound in react using usesound onloadreact play mp3 audioplay sound onload react