Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check that 'M3U8' playlist exists or not.
If playlist is not exists then auto refresh the HTML play.
If playlist exists stop the auto refresh and play the playlist in HTML5 Video tag.

My code is as follows:


What I have tried:

<!DOCTYPE html>
<html lang="en">
<head>  
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">  
      <link href="img/soft_logo.ico" rel="icon" type="image/x-icon">
      <link rel="stylesheet" type="text/css" href="css/style.css">
      <link rel="stylesheet" href="css/bootstrap.css">
      <link rel="stylesheet" href="css/bootstrap.min.css">
<!--      <script src="js/hls.js"></script>
              
      <script src="js/HlsPlayer.js" type="application/javascript"></script>-->
          
      <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
      <title>RTSP</title>
</head>
    <body class="body">
        <div class="container">
            <div class="header">
            <h1>ITS+ Live Streaming</h1>
            </div>
          </div> 
        <script>
            let xmlhttp;
            try{
                if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                } else { // code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        xmlhttp.open('HEAD', 'cam1.m3u8', false);
                        xmlhttp.send();
                    }
                }    
            if (xmlhttp.status == "404") {
                window.location.reload();            
            } else {
            if (Hls.isSupported()) {
                var video = document.getElementById('video');
                var hls = new Hls(); 
                hls.loadSource('cam1.m3u8');
                // bind them together
                hls.attachMedia(video);
                hls.on(Hls.Events.MEDIA_ATTACHED, function () {
                console.log('video and hls.js are now bound together !');
                });
                
                hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {                                                
                    video.autoplay = true;
                    video.load();
                    console.log('manifest loaded, found ' + data.levels.length + ' quality level');
                    BindData();
                });

            }
            else if (video.canPlayType('application/vnd.apple.mpegurl'))
            {
                video.src = 'cam1.m3u8';
                video.addEventListener('canplay',function(){
                            video.play();
                            });
                }
            }
        }
        catch(err){
            window.location.reload();            
        }
        </script>
        <div class="container" style="padding-top: 3%">
            <div class="row">
                <div class="col-6">
               <video id="video" class="video" autoplay(0) controls></video>
                       

                </div>
                <div class="col-3">
                    <div class="w3-panel w3-border"><h4>Camera Information</h4>
                        <p id="demo"></p>
                        
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>



When I run this code on server it does not auto play the playlist.
Posted

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