Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

play sound onload 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;
Comment

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;
Comment

play audio in react

import ReactAudioPlayer from 'react-audio-player';
//...
<ReactAudioPlayer
  src="my_audio_file.ogg"
  autoPlay
  controls
/>
Comment

play audio in react

npm install --save react-audio-player
Comment

play sound onload react

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

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

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

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

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

export default Music;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Mqtt js react-native 
Javascript :: next js cookie 
Javascript :: javascript iterate through object attributes 
Javascript :: iteratea on values map js 
Javascript :: on window resize js 
Javascript :: sequelize transaction util 
Javascript :: javascript unshift 
Javascript :: id button click jquery 
Javascript :: greater than x but less than y javascript 
Javascript :: javascript refresh element 
Javascript :: jquery toastr 
Javascript :: Substring in Javascript using substr 
Javascript :: react-data-table-component edit action 
Javascript :: how to code a discord bot in javascript 
Javascript :: arithmetic operators in javascript 
Javascript :: loading react 
Javascript :: export table data to excel using javascript or jquery 
Javascript :: how to add two times in javascript 
Javascript :: js access array in array 
Javascript :: how to connect mongodb with next js 
Javascript :: redux devtools config 
Javascript :: Ping discord 
Javascript :: react receiving socket muitple times 
Javascript :: Sort() functions 
Javascript :: update object in array state react 
Javascript :: javascript sign 
Javascript :: form contact 7 ajax send 
Javascript :: javascript escape character 
Javascript :: switch javascript 
Javascript :: xml http request fetch 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =