0

I have a simple video player, I want on page load to play the video and background audio

I tried solution on stackoverflow but nothing seems to work

Here is my solution

Html

<audio id="audioplayer" playsinline controls loop autoplay hidden>
   <source src="audio/muzyka_meed_loop.mp3" type="audio/mpeg">
   Your browser does not support the audio element.
</audio>

<div id="video-container">
   <video id="videoplayer" playsinline muted autoplay>
      <source src="videos/good_job.mp4"></source>
   </video>
</div>

Here js

 //onload play background audio
    window.onload = function() {
            document.getElementById("audioplayer").play();

     }

  //promise to play video
   if ($("#muted-btn").hasClass("muted")) {
        $("#videoplayer")[0].muted = false;

        var playPromise = $("#videoplayer")[0].play();

        if (playPromise !== undefined) {
            playPromise.then(function () {

            }).catch(function (error) {
                $("#videoplayer")[0].muted = true;
                $("#videoplayer")[0].play();
            });
        }

    } else {
        $("#videoplayer")[0].muted = false;
        var playPromise = $("#videoplayer")[0].play();

        if (playPromise !== undefined) {
            playPromise.then(function () {

            }).catch(function (error) {

                $("#videoplayer")[0].muted = true;
                var playPromise2 = $("#videoplayer")[0].play();

                if (playPromise2 !== undefined) {
                    playPromise2.then(function () {

                    }).catch(function (error) {

                    });
                }
            });
        }
    }

Here is online demo demo

On edge, safari, opera it plays but on chrome no audio.

Why audio and sound not playing on chrome???

The Dead Man
  • 6,258
  • 28
  • 111
  • 193
  • Possible duplicate of [html5 audio not playing in chrome](https://stackoverflow.com/questions/18906360/html5-audio-not-playing-in-chrome) – zod Sep 12 '19 at 14:38
  • @zod this is not duplicate , the link you provided does not have solution – The Dead Man Sep 12 '19 at 14:44
  • I've marked this as duplicate of [How to make audio autoplay on chrome](https://stackoverflow.com/questions/50490304/how-to-make-audio-autoplay-on-chrome) – the problem is that Chrome has a policy that audio will only play if it is prompted by a user action, seehttps://developers.google.com/web/updates/2017/09/autoplay-policy-changes – Patrick Hund Sep 12 '19 at 14:48
  • duplicated question ! it's because privacy but with searching in this website you can find different tricks – Alex Sep 12 '19 at 14:53

0 Answers0