sampotts / plyr

A simple HTML5, YouTube and Vimeo player
https://plyr.io
MIT License
26.63k stars 2.93k forks source link

Mute the audio if autoplay is enabled #1112

Open friday opened 6 years ago

friday commented 6 years ago

autoplay often doesn't work if the media has audio and isn't muted.

We could handle this by automatically muting when autoplay is true, then try to unmute when it has started playing (unless it's explicitly muted). This should make autoplay work (muted) in case it doesn't now, and shouldn't affect currently supported browsers.

If the latter doesn't work we could possibly add a new larger button for unmuting. I believe I've seen this behavior at social media video players somewhere. Maybe facebook?

I consider this to be a breaking change since users may prefer it to stay paused in these cases, but perhaps this could be added to v4?

andrei-pastukhou commented 6 years ago

hi, according to autoplay policy by chrome it no matter if video muted or not. user iteraction on page is important to chrome browser. Link to proof it https://developers.google.com/web/updates/2017/09/autoplay-policy-changes. According to autoplay policy of safari browser, there is autoplay depend of user's settings (there are three way, absent autuplay, allow autoplay only muted video, always autoplay). Below my design of this problem:

export default function doAutoplay(player) {
    const isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);
    const isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);

    if (isSafari || isChrome) {
        window.addEventListener('unhandledrejection', (event) => {
            event.preventDefault();
            player.muted = false;
        });
        window.requestAnimationFrame(() => {
            const availablePlay = player.elements.wrapper.querySelector('video').play();
            if (availablePlay !== undefined) {
                availablePlay.then(() => {
                }).catch(() => {
                    player.muted = true;
                    player.player.play();
                });
            }
        });
    } else {
        player.play();
    }
}
friday commented 6 years ago

From the link (it's the same as mine btw):

Muted autoplay is always allowed