How to toggle play/pause in ReactJS with audio ? Last Updated : 29 Nov, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to create a play/pause button for an audio file using ReactJS. Prerequisites:NodeJS or NPMReact JSApproach: Take the reference of the audio file in ReactJS Using Audio Class Set the default state of the song as not playing.Make a function to handle the Play/Pause of the song.Use the play() and pause() functions of the audio class to do these operations.let song = new Audio(my_song);song.play();song.pause();Steps to Create the React Application And Installing Module:Step 1: Create React App command npx create-react-app foldernameStep 2: After creating your project folder, i.e., folder name, move to it using the following command: cd foldernameProject Structure: The updated dependencies in package.json file will look like: "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4",}Example: Now write down the following code in the App.js file. JavaScript import React, { Component } from "react"; // Import your audio file import song from "./static/a.mp3"; class App extends Component { // Create state state = { // Get audio file in a variable audio: new Audio(song), // Set initial state of song isPlaying: false, }; // Main function to handle both play and pause operations playPause = () => { // Get state of song let isPlaying = this.state.isPlaying; if (isPlaying) { // Pause the song if it is playing this.state.audio.pause(); } else { // Play the song if it is paused this.state.audio.play(); } // Change the state of song this.setState({ isPlaying: !isPlaying }); }; render() { return ( <div> {/* Show state of song on website */} <p> {this.state.isPlaying ? "Song is Playing" : "Song is Paused"} </p> {/* Button to call our main function */} <button onClick={this.playPause}> Play | Pause </button> </div> ); } } export default App; Step to Run Application: Run the application using the following command from the root directory of the project: npm startOutput: Now open your browser and go to http://localhost:3000 Comment More infoAdvertise with us Next Article How to implement toggle using ReactJS ? P pratikraut0000 Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to Play/Pause video or Audio in ReactJS ? Adding multimedia elements, such as videos and audios, to your ReactJS applications can enhance the overall user experience. One common requirement is to provide controls for playing and pausing these media files. In this article, we'll explore how to implement play/pause functionality for videos an 3 min read How to Play/Pause video or Audio in camera application in ReactJS ? In this article, we'll construct a basic Respond application that permits clients to control their video and sound transfers with on/off buttons. We'll utilize symbols to address the video and sound controls for a more easy-to-use interface. This can be valuable for applications like video conferenc 4 min read How to implement toggle using ReactJS ? Toggles are a common UI element used to switch between two states or options in a user interface. In ReactJS, it can be achieved using state management and event handling. In this article, we will explore how to implement a toggle feature using ReactJS. PrerequisitesState management in ReactEvent Ma 1 min read How to record and play audio in JavaScript ? JavaScript is a very flexible language and it provides many API support to do many useful things. Here one important thing is that record audio or video in web pages is also done using JavaScript. In this case, it will ask the user for microphone access to the browser and record the audio through th 4 min read How to record and play audio in JavaScript ? JavaScript is a very flexible language and it provides many API support to do many useful things. Here one important thing is that record audio or video in web pages is also done using JavaScript. In this case, it will ask the user for microphone access to the browser and record the audio through th 4 min read How to record and play audio in JavaScript ? JavaScript is a very flexible language and it provides many API support to do many useful things. Here one important thing is that record audio or video in web pages is also done using JavaScript. In this case, it will ask the user for microphone access to the browser and record the audio through th 4 min read Like