From 29466f616f998a0979977a45d39d8faf2df870a5 Mon Sep 17 00:00:00 2001
From: xc <double72cheng@gmail.com>
Date: Mon, 26 Apr 2021 17:10:45 +0800
Subject: [PATCH] 插件化 直播

---
 src/main/resources/public/js/live/livepler.js |  158 +++++++++++++++++++++++++---------------------------
 1 files changed, 76 insertions(+), 82 deletions(-)

diff --git a/src/main/resources/public/js/live/livepler.js b/src/main/resources/public/js/live/livepler.js
index cfa787c..2c8268c 100755
--- a/src/main/resources/public/js/live/livepler.js
+++ b/src/main/resources/public/js/live/livepler.js
@@ -1,96 +1,90 @@
-!(function () {
+;!(function () {
 
-
-    window.onkeydown = function () {
-        h5lc.fullscreen = false
-    }
-    var $toolbar = document.getElementById('toolbar');
-    var $fullscreenId = document.getElementById('fullscreenId');
+    let _content = $("<div class=\"content\">\n" +
+        "        <div class=\"player\" onmouseover=\"$('.player .control').css('display', 'block')\" onmouseout=\"$('.player .control').css('display', 'none')\">\n" +
+        "            <div class=\"player-wrapper\" id=\"container\" style=\"background-color: #0D0E1B;\">\n" +
+        "            </div>\n" +
+        "            <div class=\"control\">\n" +
+        "                <div class=\"fa fa-play\" id=\"play\"></div>\n" +
+        "                <div class=\"fa fa-stop\" id=\"stop\" style=\"display: none\"></div>\n" +
+        "                <div class=\"timer\">\n" +
+        "                    <span class=\"progress_timer\">00:00:00</span>\n" +
+        "                    <span class=\"duration_timer\">00:00:00</span>\n" +
+        "                </div>\n" +
+        "                <div class=\"fa fa-expand expand\" onclick=\"h5lc.fullscreen=true\"></div>\n" +
+        "                <div class=\"fa fa-close\" id=\"close\"></div>\n" +
+        "            </div>\n" +
+        "        </div>\n" +
+        "    </div>");
+    $("#jessibucaContainer").append(_content);
 
     var $play = document.getElementById('play');
     var $stop = document.getElementById('stop');
-    var container = document.getElementById("container");
 
-    var isPlaying = false;
-    let source = queryParam('source') || 'localhost';
+    var $jessibucaContainer = document.getElementById('jessibucaContainer');
 
-    var h5lc = new Jessibuca({container, decoder: "../js/jessibuca/ff.js", videoBuffer: 1});
 
-    h5lc.onLoad = function () {
-        //this.play("wss://pulls.1234326.cn/live/L01.flv")
-        //this.play("ws://localhost:8080/live/test.flv")
-        //this.play("ws://" + source + ":8080/live/" + stream)
-        //this.play("ws://pull3.afb1188.com/live/" + stream + ".flv")
-        //this.play("ws://pull2.afb1188.com/live/" + stream + ".flv")
-    }
-
-    //自动加载播放
-    h5lc.onPlay = function () {
-        isPlaying = true;
-        $play.style.display = 'none';
-        $stop.style.display = 'inline';
-    }
-
-    //点击播放按钮播放
-    $play.addEventListener('click', function () {
-        isPlaying = true;
-        h5lc.play("ws://172.16.30.113:8082/live/ff80808178af424e0178af44e677000343616d657261.flv")
-        $play.style.display = 'none';
-        $stop.style.display = 'inline';
-    }, false);
-
-    //点击播放停止
-    $stop.addEventListener('click', function () {
-        isPlaying = false;
-        //stop();
-        $play.style.display = 'inline';
-        $stop.style.display = 'none';
-    }, false);
-
-    function stop() {
-        $.get("http://127.0.0.1:8088/monica/video/stop?param=live/test", function () {
-                h5lc.close()
-            }
-        );
-    }
-
-    function queryParam(name, url) {
-        var search = window.location.search;
-        var qArr = '';
-        var key = {};
-        if (url) {
-            qArr = url.split("?")[1].split("&")
-        } else {
-            if (!window.location.search) {
-                return
-            }
-            qArr = search.substr(1).split("&")
+    function Livepler(opt) {
+        if (typeof opt.elem === 'string') {
+            opt.elem = document.getElementById(opt.elem);
         }
-        for (var i = 0; i < qArr.length; i++) {
-
-            var firstPos = qArr[i].indexOf('=');
-            key[qArr[i].slice(0, firstPos)] = qArr[i].slice(firstPos + 1)
+        if (!opt.elem) {
+            throw new Error('需要容器');
+            return;
         }
-        if (name) {
-            return key[name]
-        } else {
-            if (url) {
-                return key[name]
-            } else {
-                return key
+        let stream_path;
+
+        this.elem = opt.elem;
+        let ffurl = opt.ffurl;
+        let code = opt.code;
+        this.container = document.getElementById("container");
+
+        let h5lc = new Jessibuca({container, decoder: ffurl, videoBuffer: 0});
+        this.playVideo(code,function (result) {
+             h5lc.onLoad=function () {
+                this.play(result.jessicaURL);
             }
+            stream_path=result.streamPath;
+            $play.style.display = 'none';
+            $stop.style.display = 'inline';
+        })
+
+        let $close = document.getElementById('close');
+        let than = this;
+        //关闭
+        $close.addEventListener('click', function () {
+            h5lc.destroy();
+            than.closeVideo(stream_path)
+            than.elem.style.display = 'none';
+        }, false);
+    }
+
+    Livepler.prototype.playVideo = function (video_id, callback) {
+        $.ajax({
+            type: "get",
+            //url: "../../webapi/media/live?param=" + video_id,
+            url: "../../monica/live?param=" + video_id,
+            timeout: 5000,
+            async: false,
+            success: function (result) {
+                callback(result);
+            }
+        });
+    }
+
+    Livepler.prototype.closeVideo = function (streamPath) {
+        if (streamPath && typeof streamPath === "string") {
+            $.ajax({
+                type: "get",
+                //url: "../../webapi/media/stop?param=" + streamPath,
+                url: "../../monica/stop?param=" + streamPath,
+                async: false,
+                success: function (result) {
+                }
+            });
         }
     }
 
-    //工具栏 鼠标定位隐藏
-    $toolbar.onmouseover(function () {
-        $(".player .control").css("display", "block")
-    })
-    $toolbar.onmouseout(function () {
-        $(".player .control").css("display", "none")
-    })
-    $fullscreenId.onclick(function () {
-        h5lc.fullscreen=true
-    })
+    window.Livepler = Livepler;
+})()
 
-})();

--
Gitblit v1.9.1