Drew-Mace
Drew-Mace

Reputation: 31

Typescript function to mute HTML audio

I have audio that auto-plays on screen load. I am trying to include a button that will mute the audio using Typescript. The app is built in Angular4. I have tried using

document.getElementById('mute').addEventListener('click', function (e)
{
e = e || window.event;
audio.muted = !audio.muted;
e.preventDefault();
}, false);

but am getting Event is not assigned to MouseEvent, it also says muted does not exist on type HTMLElement.

Is there a way to do this in the main.ts file?

Upvotes: 2

Views: 1581

Answers (1)

Saravana
Saravana

Reputation: 40642

The muted property is present on the HTMLMediaElement interface. You have to use type assertion to assert that audio is of type HTMLMediaElement type.

let audio = document.getElementById('your-audio-element') as HTMLMediaElement
// ...
audio.muted = true;

Upvotes: 4

Related Questions