Warm tip: This article is reproduced from serverfault.com, please click

HTML5 audio tag in React

发布于 2017-03-26 19:03:21

I want to use a simple HTML5 audio tag in my react application. My component looks like this:

import React from 'react'

class AudioPlayer extends React.Component {
  render() {
    return (
      <div>
        <audio ref="audio_tag" src="./static/music/foo.mp3" controls autoPlay/>
      </div>
    );
  }
}

export default AudioPlayer;

but it just won't play. I also don't get any errors. I have put the mp3 file in every single folder in my app now, and tried different paths etc. but it just does not seem to recognise it. Am I missing something trivial maybe?

Questioner
George Welder
Viewed
0
George Welder 2017-06-15 00:11:42

I have accidentally used a broken audio file, so this explains why my solution did not "work". The code above is, as Jordan Running points out, fine, so I'll leave this as the answer.