I just want to add background music that will play continuously. Everything, I got is a player and I need to click the play button or any button. That’s what I don’t want. In HTML is easy. But how about React.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.
Adding continuous background music to a React app can be achieved using the HTML5 <audio> tag in combination with React state and lifecycle methods to control playback. Using the ‘autoPlay’ attribute will allow the audio to start once the component has mounted.
- import React from 'react';
-
- class MusicPlayer extends React.Component {
-
- render() {
- return (
-
-
- );
- }
- }
Note: Autoplaying sound can provide a poor user experience, and many browsers have settings to limit or disable autoplaying. Mobile browsers generally do not allow autoplaying sound at all. Therefore, it’s best to give your users control over sound on your site.
For more information on how to use these tags and attributes in a more advanced way, you can follow React’s guide on uncontrolled components
Hope that this helps!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.