2

So I have 2 fields that can be filled in the UI and it works but how can I fill the UI programmatically.

<div class="form-group">
  <label for="durationText">Song Duration (ms)</label>
  <input type="text" class="form-control" [(ngModel)]="durationText" id="durationText" placeholder="Song Duration">
</div>

<div class="form-group">
    <label for="pitchfile">Pitches file (.txt)</label>
    <input type="file" class="form-control-file" id="pitchfile" accept=".txt" (change)="setPitchfile($event)">
</div>

enter image description here

And in the .ts file I have:

durationText : "";
pitchfile: any;

And i want to set it like:

durationText = "120";

But it does not let me so what should I do ? And how do I set the file pitchfile programmatically.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Duration is just a string in my example (or atleast should be) and when I try to set it with this.durationText = "231". I get this error : "TS2322: Type '"231"' is not assignable to type '"". And the pitchfile would be an actual file which I would get from the server. I am sorry for not giving enough information in the beginning.
durationText : ""; should have been durationText : string; This would fix the error you're getting.
Your answer does a bit too much but I can use it and replace the duration. The point of my question was to how to set duration programmatically and how to set the file the same way. Without interacting with the UI.
For that I guess you'll have to read the txt file and then based on that set the durationText. What exactly is the content of your file?
pitchfile would be .mp3. Also by chaning it to durationText : string; resolved my issue, sorry if that seemed a little dumb.
|
1

This is very simple, you need to add 'name' attribute to your input like this:

<input type="text" class="form-control" [(ngModel)]="durationText" id="durationText" name="durationText" placeholder="Song Duration">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.