1

How I can get media.duration to live in a state environment ?

Right now I don't have access to it, even after trying to change state

  handleClick(e) {
        e.preventDefault();

    var reader = new FileReader();
    reader.readAsDataURL(this.state.uploadedFile);
    reader.onload = function() {
      var media = new Audio(reader.result);
      media.onloadedmetadata = function() {
        console.log(media.duration); // <-- THIS WORKS!!!!!!!!!!!!!!
      };
    };
    console.log(media.duration) //<------THIS DOES NOT WORK!!!!!!!!!!
    console.log("The link was clicked.");
    console.log(this.state.duration);
}

from: How to get duration of video when I am using filereader to read the video file?

_________________________________________

I tried this but it did not work

    this.setState({duration:media.duration},()=>{
console.log(this.state.duration)
})
Community
  • 1
  • 1
azuldev
  • 570
  • 6
  • 10

1 Answers1

0

It's because you use media variable is out of the scope. Please try this one.

handleClick(e) {
    e.preventDefault();
    var reader = new FileReader();
    reader.readAsDataURL(this.state.uploadedFile);
    reader.onload = () => {
      var media = new Audio(reader.result);
      media.onloadedmetadata = () => {
        console.log(media.duration); // <-- THIS WORKS!!!!!!!!!!!!!!
        this.setState({duration: media.duration});
      };
    };
}
freedev111
  • 132
  • 4