Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Get <audio> element reference

I have an audio tag in my template and I want to access these methods inside my component, cause I need functions for play and pause according to click events.

I tried the way below, but I can't access play() method. What is the correct approach?

TEMPLATE:

<div align="center" class="tela">
<div class="telaPlay" *ngIf="showPlay">
  <p>
    Qual é<br/>a<br/>Música?
  </p>  
  <a href="#"
    class="botao"
    (click)="novoJogo()">Play
  </a>
</div>  

COMPONENT ...relevant...:

  play(): void {
    let player = document.getElementById("player");
    player.play();
  }
}

CAN'T ACCESS JS METHODS

enter image description here

like image 577
nanquim Avatar asked Sep 17 '25 07:09

nanquim


1 Answers

Typecast element to HTMLAudioElement

function play(): void {
    let player = <HTMLAudioElement>document.getElementById("player");
    player.play();
}
like image 98
Deepak Kumar T P Avatar answered Sep 18 '25 21:09

Deepak Kumar T P