I'm working on a function that can read the duration of a audio file input and then write it in the state. The getDuration
function works and I can console log the duration of the audio files. However, I have problems to access the duration outside the getDuration
function.
I would like to do something like this:
onChangeAudioFile(e) {
this.setState({
selectedFileDuration: getDuration(e.target.files[0])
};
function getDuration(file) {
var objectURL = URL.createObjectURL(file);
var audio = new Audio([objectURL]);
var duration = null
audio.onloadedmetadata = function() {
console.log(audio.duration);
duration = audio.duration;
}
return duration
}
}
I'm able to console log the duration, but I can not bring the value out of the function and write it in my state.
I am happy for any clarification.
Greetings