0

I want to play my selected file with audio tag:

  let [selectedFile, setSelectedFile] = useState();


  const changeHandlerFile = (event) => {
    setSelectedFile(event.target.files[0]);
}

<input type="file" name="file" onChange={changeHandlerFile} accept="audio/*" />

<audio src={selectedFile} controls />

selectedFile variable is selected from an input file type.

but it doesn't play anything.

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
  • What does `selectedFile` result in? Is it a URL? What file does it try and load? – evolutionxbox Aug 17 '21 at 13:08
  • Does this answer your question? [React: How to read audio file and provide preview before upload?](https://stackoverflow.com/questions/64609592/react-how-to-read-audio-file-and-provide-preview-before-upload) – Matt U Aug 17 '21 at 13:14
  • The duplicate link I shared should do the trick. The reason your current solution doesn't work as-is, is the fact that the file input itself doesn't really expose any information about the file other than file name. This is for security purposes, so a web app can't blindly read files from the client machine. – Matt U Aug 17 '21 at 13:18

0 Answers0