~hristoast/hristoast

cf62424c90b94600840a2e50c9aedc4740e687d8 — Hristos N. Triantafillou 3 years ago 5f77389
Try to show the status of the stream

Doing this also makes it so the video isn't loaded when the stream
isn't live.
3 files changed, 44 insertions(+), 14 deletions(-)

M site/css/site.css
M site/js/stream.js
M site/video/stream.html
M site/css/site.css => site/css/site.css +4 -0
@@ 137,6 137,10 @@ div.entry {
    text-decoration: none;
}

.red {
    color: red;
}

@media (min-width: 768px) and (max-width: 1024px),(min-width: 481px) and (max-width: 767px),(min-width: 320px) and (max-width: 480px) {
    body {
        width: 95%;

M site/js/stream.js => site/js/stream.js +36 -14
@@ 16,23 16,45 @@
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var stat = document.getElementById("stat");
var video = document.getElementsByTagName("video")[0];
var vidsrc = "https://vs.hristos.net/vs/dagoth-ur.m3u8";

function loadHLS() {
    if(Hls.isSupported()) {
        var hls = new Hls();
        hls.loadSource(vidsrc);
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED, function() {
            video.play();
        });
    }
    else if (video.canPlayType('application/vnd.apple.mpegurl')) {
        video.src = vidsrc;
        video.addEventListener('loadedmetadata', function() {
            video.play();
        });
    }
    var request = new XMLHttpRequest();
    request.open("GET", vidsrc, true);

    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
            stat.textContent = "The stream is live!";
            stat.style.color = "green";

            if (Hls.isSupported()) {
                var hls = new Hls();
                hls.loadSource(vidsrc);
                hls.attachMedia(video);
                hls.on(Hls.Events.MANIFEST_PARSED, function() {
                    video.play();
                });
            }

            else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = vidsrc;
                video.addEventListener('loadedmetadata', function() {
                    video.play();
                });
            }

        } else {
            // The stream is offline at this time.
        }
    };

    request.onerror = function() {
        // The stream is offline at this time.
    };

    request.send();
}

window.onload = loadHLS();

M site/video/stream.html => site/video/stream.html +4 -0
@@ 1,4 1,8 @@
<h3>Live Video Stream</h3>
<p>Sometimes I do things and stream video of it.</p>

<p class="bold center"><noscript>If you allow javascript, the text below updates when the stream is live.</noscript></p>

<h3 id="stat" class="bold center red">The stream is currently offline!</h3>

<video id="stream" controls poster="/i/soon.jpg" title="The live video stream should be here.  Refresh the page if you have playback issues."></video>