Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

Using the HTML5 audio tag in Reactjs

I'm trying to use the HTML5 <audio> tag in React but it isn't being rendered.

This is my first time using React, so I might be missing something obvious.

I'm taking an existing Drupal frontend and re-building it in React.

All my other HTML is working, but the HTML5 <audio> tag is not.

Here's my simplest component to show the problem:

  const NoData = () => (
    <audio controls="" controlsList="nodownload">
      <source src="/sites/default/files/fruits/apples.mp3" type="audio/mpeg" />
    </audio>
  );

When I view the page, the HTML is there, but it doesn't get rendered.

This HTML was copied from another part of my site verbatim, so it should work.

The React app is embedded in a Drupal page, so I copied the <audio> tag into the Drupal page itself, and it works there. It just doesn't show up inside React itself.

The file in the <source> tag is being read; if I change the file to one that is invalid, I get a warning in the Chrome dev tools.

So how do I use the HTML5 <audio> tag in React?

question from:https://stackoverflow.com/questions/65599275/using-the-html5-audio-tag-in-reactjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just leave controls like this:

const App = () => (
  <div>
    <audio src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3" controls />
    <audio controlsList="nodownload" controls>
     <source src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3" type="audio/mpeg" />
    </audio>
  </div>
)
ReactDOM.render(<App />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...