index_danmu.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>NodePlayer.js Demo</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  7. <style>
  8. body {
  9. padding: 0;
  10. margin: 0;
  11. }
  12. .box {
  13. margin: 8px 0;
  14. display: flex;
  15. }
  16. .fl1 {
  17. flex: 1;
  18. }
  19. .fl0 {
  20. flex: 0;
  21. }
  22. .danmustage {
  23. width: 100%;
  24. height: 100%;
  25. position:absolute;
  26. z-index: 2;
  27. color: #fff;
  28. overflow: hidden;
  29. font-size: 20px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div style="max-width:980px;margin:0 auto">
  35. <h3><a href="http://www.nodemedia.cn" target="_blank">NodePlayer.js Demo</a></h3>
  36. <div style="width:100%; padding-bottom:56%;position:relative;z-index: 1;">
  37. <canvas id="video1" style="width:100%;height:100%;position:absolute;background-color: black;"></canvas>
  38. <div class="danmustage"></div>
  39. </div>
  40. <div>
  41. <div class="box">
  42. <input id="url" class="fl1" value="http://192.168.0.2:8000/live/bbb.flv" />
  43. <button class="fl0" onclick="startFunc()">Play</button>
  44. <button class="fl0" onclick="stopFunc()">Stop</button>
  45. <button class="fl0" onclick="fullFunc()">Full</button>
  46. <button class="fl0" onclick="screenshot()">ScreenShot</button>
  47. </div>
  48. <div class="box">
  49. <div class="fl1">
  50. <label>Volume:</label>
  51. <select onchange="volumeChange(event);">
  52. <option>100</option>
  53. <option>75</option>
  54. <option>50</option>
  55. <option>25</option>
  56. <option>0</option>
  57. </select>
  58. </div>
  59. <div class="fl1">
  60. <label>BufferTime:</label>
  61. <select id="buffertime" onchange="bufferChange(event);">
  62. <option>0</option>
  63. <option>100</option>
  64. <option>300</option>
  65. <option>500</option>
  66. <option selected>1000</option>
  67. <option>2000</option>
  68. <option>3000</option>
  69. </select>
  70. </div>
  71. <div class="fl1">
  72. <label>ScaleMode:</label>
  73. <select onchange="scaleModeChange(event);">
  74. <option selected>0</option>
  75. <option>1</option>
  76. <option>2</option>
  77. </select>
  78. </div>
  79. <div class="fl1">
  80. <label>CryptoKey:</label>
  81. <input id="key" class="fl1" value="" />
  82. </div>
  83. </div>
  84. <!-- /input-group -->
  85. </div>
  86. </div>
  87. <!-- <script src="//game.gtimg.cn/images/js/jquery/jquery-1.11.3.min.js"></script> -->
  88. <script src="//game.gtimg.cn/images/js/zepto/zepto.min.js"></script>
  89. <script src="//game.gtimg.cn/images/js/su/danMu.beta.min.js"></script>
  90. <script src="https://cdn.bootcdn.net/ajax/libs/vConsole/3.3.4/vconsole.min.js"></script>
  91. <script type="text/javascript" src="./NodePlayer.min.js"></script>
  92. <script>
  93. if (/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)) {
  94. //手机开启vconsole,便于查看控制台调试信息,正式部署时无需使用,包括上面的vconsole.min.js也不用引入
  95. var vConsole = new VConsole();
  96. }
  97. //初始的弹幕数据
  98. var danmuData = [
  99. '哈哈哈哈哈哈哈',
  100. '弹幕来袭~',
  101. '前面的 你肥皂掉了!!',
  102. "前方高能,一大波弹幕来袭!",
  103. "不约,叔叔我们不约~",
  104. "今天没吃药,感觉自己萌萌哒",
  105. "走开,让专业的来",
  106. "23333333333~~~~~",
  107. "请允许我做一个悲伤的表情",
  108. "警察叔叔,就是这个人",
  109. "为什么放弃治疗",
  110. "放开那个人,让我来",
  111. "噗……节操去了哪里啊都!!",
  112. "膜拜老司机",
  113. "你咋不上天呢",
  114. "666",
  115. "666666666666666666666666~~~",
  116. "放开她,让我来!!!",
  117. "德里克。。。。test 。。。",
  118. "666~~~~~~~~~!!!!!",
  119. "前方高能 非战斗人员紧急撤离",
  120. "妈妈问我为什么跪着看电脑_(:_」∠)_",
  121. "他说的好有道理 我竟无言以对",
  122. '小六 你好靓吖',
  123. '我基本是只废猫(≥◇≤)',
  124. '这个是真的耶',
  125. '弹幕走一波~~~~~~~~~~~~~~~~~~~~~~~~~~',
  126. '我似乎听到有人说我帅',
  127. '什么?!你刚才说我帅?',
  128. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~我是天空中最亮的星星~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~',
  129. '好多人啊',
  130. '喂,妖妖灵吗,这里有人在装逼。。',
  131. '对方不想和你说话,并向你扔了一条狗。。。',
  132. '前面那个说我帅的别走啊啊啊啊',
  133. '你们玩,我静静地看着',
  134. '我就静静看着你',
  135. '超级羞羞',
  136. '好想你',
  137. '我想你!',
  138. '你真帅~',
  139. '你肚子好大!!',
  140. '最近变胖了,悲伤'
  141. ];
  142. //实例化弹幕
  143. var dm = new danMu({
  144. device: 'm',//m代表移动端弹幕,不设置或设为pc代表pc端弹幕
  145. stage: $(".danmustage"),
  146. comments: danmuData,
  147. danMuHtml: "<span>{comment}</span>",
  148. colors: ["#fff", "#00ccff", "#ff0000", "#00ff00", "#fef500"],
  149. flyTime: 7000,
  150. timeInterval: 1200,
  151. randomSort: true,
  152. hSpacing: 0.8
  153. });
  154. dm.pause();
  155. var url = document.getElementById("url");
  156. var key = document.getElementById("key");
  157. var argurl = getQueryVariable("url");
  158. if (argurl) {
  159. url.value = argurl;
  160. }
  161. var argkey = getQueryVariable("key");
  162. if (argkey) {
  163. key.value = argkey;
  164. }
  165. /**
  166. * 是否打印debug信息
  167. */
  168. // NodePlayer.debug(true);
  169. var player;
  170. // 0.5.28之后, 为了统一asm与wasm版本api差异,现统一采用回调格式加载.
  171. NodePlayer.load(() => {
  172. player = new NodePlayer();
  173. /**
  174. * 自动测试浏览器是否支持MSE播放,如不支持,仍然使用软解码。
  175. * 紧随 new 后调用
  176. * 不调用则只使用软解
  177. */
  178. // player.useMSE();
  179. /**
  180. * 使用worker线程解码, 适用于多画面直播, 能有效利用多核处理器
  181. * 紧随 new 后调用
  182. */
  183. player.useWorker();
  184. /**
  185. * 开启屏幕常亮
  186. * 在手机浏览器上,canvas标签渲染视频并不会像video标签那样保持屏幕常亮
  187. * 如果需要该功能, 可以调用此方法
  188. * H5目前在chrome\edge 84, android chrome 84及以上有原生亮屏API, 需要是https页面
  189. * 其余平台为模拟实现,非全兼容。
  190. */
  191. player.setKeepScreenOn();
  192. /**
  193. * 传入 canvas视图的id,当使用mse时,自动转换为video标签
  194. */
  195. player.setView("video1");
  196. /**
  197. * 设置最大缓冲时长,单位毫秒,只在软解时有效
  198. */
  199. player.setBufferTime(1000);
  200. /**
  201. * 设置超时时长, 单位秒,只在软解时有效
  202. * 在连接成功之前和播放中途,如果超过设定时长无数据返回,则回调timeout事件
  203. */
  204. // player.setTimeout(10);
  205. player.on("start", () => {
  206. console.log("player on start");
  207. });
  208. player.on("stop", () => {
  209. console.log("player on stop");
  210. });
  211. player.on("error", (e) => {
  212. console.log("player on error", e);
  213. });
  214. player.on("videoInfo", (w, h, codec) => {
  215. console.log("player on video info width=" + w + " height=" + h + " codec=" + codec);
  216. });
  217. player.on("audioInfo", (r, c, codec) => {
  218. console.log("player on audio info samplerate=" + r + " channels=" + c + " codec=" + codec);
  219. });
  220. // player.on("videoSei", (sei, pts) => {
  221. // console.log("player on video sei=" + sei + " pts=" + pts);
  222. // });
  223. // player.on("videoFrame", (pts) => {
  224. // console.log("player on videoFrame pts=" + pts);
  225. // });
  226. // player.on("timeout", () => {
  227. // console.log("player on timeout");
  228. // player.stop();
  229. // });
  230. // player.on("buffer", (state) => {
  231. // console.log("player on buffer state=" + state);
  232. // });
  233. player.on("stats", (stats) => {
  234. console.log("player on stats=", stats);
  235. });
  236. });
  237. function startFunc() {
  238. /**
  239. * 设置解密密码,必须是16字节
  240. */
  241. player.setCryptoKey(key.value);
  242. /**
  243. * 开始播放,参数为 http-flv或 websocket-flv 的url
  244. */
  245. player.start(url.value);
  246. dm.play();
  247. }
  248. function stopFunc() {
  249. /**
  250. * 停止播放
  251. */
  252. player.stop();
  253. //按需清理画布为黑色背景
  254. // player.clearView();
  255. dm.pause();
  256. }
  257. function fullFunc() {
  258. player.fullscreen();
  259. }
  260. function volumeChange(event) {
  261. /**
  262. * 设置音量
  263. * 0.0 ~~ 1.0
  264. * 当为0.0时,完全静音, 最大1.0
  265. */
  266. player.setVolume(event.target.value / 100.0);
  267. }
  268. function bufferChange(event) {
  269. player.setBufferTime(event.target.value);
  270. }
  271. function scaleModeChange(event) {
  272. /**
  273. * 视频缩放模式, 当视频分辨率比例与Canvas显示区域比例不同时,缩放效果不同:
  274. * 0 视频画面完全填充canvas区域,画面会被拉伸 --- 默认值
  275. * 1 视频画面做等比缩放后,对齐Canvas区域,画面不被拉伸,但有黑边
  276. * 2 视频画面做等比缩放后,完全填充Canvas区域,画面不被拉伸,没有黑边,但画面显示不全
  277. * 软解时有效
  278. */
  279. player.setScaleMode(event.target.value);
  280. }
  281. function screenshot() {
  282. // player.screenshot("np_screenshot.png", "png");
  283. player.screenshot("np_screenshot.jpeg", "jpeg", 0.8);
  284. }
  285. function getQueryVariable(variable) {
  286. var query = window.location.search.substring(1);
  287. var vars = query.split("&");
  288. for (var i = 0; i < vars.length; i++) {
  289. var pair = vars[i].split("=");
  290. if (pair[0] == variable) { return pair[1]; }
  291. }
  292. return (false);
  293. }
  294. </script>
  295. </body>
  296. </html>