Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: restart the html audio tag on real time

Tags:

html

angular

I have a component which play an mp3 file, and it gets the played file name from its parent. Here is the code:

export class PlayComponent implements OnChanges  {

      @Input() fileToPlay:string;

      ngOnChanges(arg){

         console.log(arg.fileToPlay);
      }
    }

and the html is:

<div *ngIf="fileToPlay!=''">
    <audio  controls autoplay class="playa" di="audio">
      <source [src]="fileToPlay" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
</div>

It works fine for first play. The value of fileToPlay may change and i want to play the new file on real time, but it always play the first file name.

How can i fix it?

like image 387
Sal-laS Avatar asked Dec 24 '16 10:12

Sal-laS


People also ask

How do I turn off audio in HTML?

Audio pause() Method The pause() method halts (pauses) the currently playing audio. Tip: This method is often used together with the play() method.

Can I play audio in HTML True or false?

HTML Audio - Methods, Properties, and EventsThis allows you to load, play, and pause audios, as well as set duration and volume. There are also DOM events that can notify you when an audio begins to play, is paused, etc. For a full DOM reference, go to our HTML Audio/Video DOM Reference.

How do you embed an audio file in HTML?

To embed audio in HTML, we use the <audio> tag. Before HTML5, audio cannot be added to web pages in the Internet Explorer era. To play audio, we used web plugins like Flash.


1 Answers

I know this is old but I had the same problem and the only thing that worked for me was putting the src in the audio not the source:

<audio controls src={{filetoplay}}></audio>

couldn't find anything on google so figured it out myself, hope it helps someone

like image 94
Jan V. Avatar answered Sep 26 '22 03:09

Jan V.