UPDATE 1:
There's an issue with your code.
durationText : "";
pitchfile: any;
Should have been:
durationText : string;
pitchfile: any;
UPDATE 2:
In case this was a text file that you were accepting and the user selected the file via an input type file, you could read the contents of it using this:
import { Component } from '@angular/core';
@Component({...})
export class AppComponent {
durationText;
setPitchfile($event) {...}
readFromTextFile($event) {
const file = $event.target.files[0];
var reader = new FileReader();
reader.onload = function () {
console.log(reader.result);
};
reader.readAsText(file);
}
...
}
The Sample StackBlitz is updated accordingly.
ORIGINAL ANSWER:
Not really sure how you'll be able to get the duration from a text file.
But if you have an actual audio file, you can do something like this:
You can create a new Audio() instance and then set it's src property using URL.createObjectURL($event.target.files[0]);. Once the metadata is loaded, the Audio instance fires a loadedmetadata event to which you can listen to assigning a function to the onloadedmetadata on the audio instance.
Inside this callback function, you can check for the duration property.
Give this a try:
import { Component } from '@angular/core';
@Component({...})
export class AppComponent {
durationText = '';
setPitchfile($event) {
console.log($event.target.files);
const audio = new Audio();
audio.src = URL.createObjectURL($event.target.files[0]);
audio.onloadedmetadata = () => {
console.log(audio.duration);
this.durationText = audio.duration;
};
}
...
}
Here's a Working Sample Stackblitz for your ref.