You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NWJS应用中hls.js流媒体代码无法运行,但Chrome浏览器可正常运行

NWJS中hls.js播放HLS流媒体失效问题

我正在开发一款基于NWJS的HLS流媒体应用,使用hls.js库实现HLS播放(Chrome无原生HLS支持)。当前遇到的问题:

  • 在NWJS 0.94.0 SDK版本中运行时,页面加载后video元素仅显示从白到灰的加载渐变,并未开始流播放;点击播放按钮后,video元素改变尺寸但仅显示黑屏
  • 直接在Chrome浏览器中打开项目的index页面,代码可正常运行:页面加载时视频自动开始播放,且能正常暂停、播放

HTML代码

<!doctype HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>HLS Viewer</title>
  <script type="text/javascript" src="../javascript/hls.js"></script>
  <script type="text/javascript" src="../javascript/hls_init.js"></script>
</head>

<body>
  <video id="test_video" controls></video>
</body>
</html>

JS初始化脚本

function initHLS() {
  let video = document.getElementById('test_video');

  var hls = new Hls({
    debug: true,
  });

  hls.loadSource('https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8');

  hls.attachMedia(video);
  hls.on(Hls.Events.MEDIA_ATTACHED, function () {
    video.muted = true;
    video.play();
  });
}

window.addEventListener("DOMContentLoaded", initHLS);

package.json配置

{
  "name": "CamViewNWJS2",
  "description": "IP Camera Viewer built on Nodejs",
  "version": "0.0.1",
  "icon": "icons/app.icns",
  "main": "html/main.html",
  "chromium-args": "--enable-logging=stderr",
  "window": {
    "toolbar": false,
    "width": 1500,
    "height": 900,
    "position": "center"
  },
  "nodejs": true,
  "scripts": {
    "prod": "nwbuild --platforms win64 --buildDir dist/ ./"
  },
  "devDependencies": {
    "nw": "^0.12.0",
    "nw-builder": "^3.7.0"
  }
}

项目结构

package.json
html       |- index.html
javascript |- hls_init.js
           |- hls.js

注:hls.js已从官方渠道下载并纳入项目包中,NWJS运行时生成了对应的错误日志。

请问:为什么这段在Chrome浏览器中正常运行的代码,在NWJS中无法正常工作?


内容的提问来源于stack exchange,提问作者KevinHJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:47:07