Skip to content

Commit

Permalink
add/remove muted attribute (#1283)
Browse files Browse the repository at this point in the history
Unless the attribute is actually on the element, the browser doesn't recognise it as muted and won't autoplay.
Also added missing volumechange handler to audio.
  • Loading branch information
edsilv authored Jan 17, 2025
1 parent 2187f78 commit fec1a53
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ export class MediaElementCenterPanel extends CenterPanel<
}
});

this.extensionHost.subscribe(IIIFEvents.SET_MUTED, (muted) => {
this.extensionHost.subscribe(IIIFEvents.SET_MUTED, (muted: boolean) => {
if (that.player) {
that.player.setMuted(muted);
that.updateMutedAttribute(muted);
}
});

Expand All @@ -127,6 +128,14 @@ export class MediaElementCenterPanel extends CenterPanel<
this.title = this.extension.helper.getLabel();
}

updateMutedAttribute(muted: boolean) {
if (muted) {
this.$media.attr("muted", "");
} else {
this.$media.removeAttr("muted");
}
}

async openMedia(resources: IExternalResource[]) {
const that = this;

Expand Down Expand Up @@ -278,10 +287,13 @@ export class MediaElementCenterPanel extends CenterPanel<

if (that.muted === true && muted === false) {
that.muted = false;

that.extensionHost.fire(
MediaElementExtensionEvents.MEDIA_UNMUTED
);
}

that.updateMutedAttribute(that.muted);
});
},
});
Expand Down Expand Up @@ -351,6 +363,24 @@ export class MediaElementCenterPanel extends CenterPanel<
Math.floor(mediaElement.currentTime)
);
});

mediaElement.addEventListener("volumechange", (volume) => {
const muted: boolean = volume.detail.target.getMuted();

if (that.muted === false && muted === true) {
that.muted = true;
that.extensionHost.fire(MediaElementExtensionEvents.MEDIA_MUTED);
}

if (that.muted === true && muted === false) {
that.muted = false;
that.extensionHost.fire(
MediaElementExtensionEvents.MEDIA_UNMUTED
);
}

that.updateMutedAttribute(that.muted);
});
},
});
}
Expand Down

0 comments on commit fec1a53

Please sign in to comment.