Click here to Skip to main content
15,888,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello. I'm trying to handle events of media in my PHP webpage project using Laravel framework and I'm pretty new to it. I have an audio and an video, the audio will automatically play when the webpage is loaded and I want when the video starts, the audio will stop until I pause the video.
Here is my code:
<pre>
<script>
     window.onload = function()
     {
         var mainaudio = document.getElementById("mainaudio");
         var trailervideo = document.getElementById("trailer-video");
         mainaudio.volume = 0.1;

         //If the video plays, the audio stops until the video pauses again.
         var flag = 0;
         trailervideo.onplay = function()
         {
             flag = 1;
             while (flag === 1)
             {
                 mainaudio.pause();
                 if (flag === 1)
                 {
                      mainaudio.pause();
                 }
                 else if (flag === 0)
                 {
                     mainaudio.play();
                 }
                 trailervideo.onpause = function ()
                 {
                     flag = 0;
                 }
             }
         }
     }
</script>


But when I started the webpage, the browser froze for seconds and I couldn't even know if the audio stops when the video starts. Please help!

What I have tried:

I firstly tried on Firefox and then OperaGX and even Edge Chromium (all of them are up to date) but they kept freezing unreasonably.
Posted
Updated 15-Aug-21 23:40pm

1 solution

Quote:
... the audio will automatically play when the webpage is loaded ...
Aside from annoying your users, this behaviour will almost certainly be blocked:
Autoplay guide for media and Web Audio APIs - Web media technologies | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900