2

I'd like to be able to create a function to stop or pause an HTML from my component.ts file. I'm not seeing any built in methods to pause the audio using the document.getelement

<audio controls id="audio-file" >
<source src="sample.wav" type="audio/wav">
</audio>
import { Component, OnInit, Inject } from '@angular/core';
import { DOCUMENT } from "@angular/platform-browser";
import { WINDOW } from "../../services/window.service";

@Component({
  selector: 'app-audio',
  templateUrl: './audio.component.html',
  styleUrls: ['./audio.component.css']
})
export class AudioComponent implements OnInit {
  private audio = this.document.getElementById("audio-file")
  constructor(
    @Inject(DOCUMENT) private document: Document,
    @Inject(WINDOW) private window: Window
  ) { }
  ngOnInit() {
  }
stopAudio(){
  this.document.getElementById('audio-file')
}
}

1 Answer 1

2

Call .pause() on the audio element.

stopAudio() {
  this.audio.pause();
}

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

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

2 Comments

Inspect the method, check if private audio = this.document.getElementById("audio-file") is properly setting the element, maybe you need to move that inside ngOnInit.
thank you! declaring private audio; at the top and setting it in the on init fixed my issue

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.