我目前正在尝试通过socket.io将.webm视频文件流式传输到我的客户端(目前使用Chrome作为客户端).
将第一个附加Uint8Array
到SourceBuffer工作正常但附加更多的不起作用并引发以下错误:
Uncaught DOMException: Failed to execute 'appendBuffer' on 'SourceBuffer': The HTMLMediaElement.error attribute is not null.
我目前的代码:
'use strict'; let socket = io.connect('http://localhost:1337'); let mediaSource = new MediaSource(); let video = document.getElementById("player"); let queue = []; let sourceBuffer; video.src = window.URL.createObjectURL(mediaSource); mediaSource.addEventListener('sourceopen', function() { sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"'); socket.on("video", function(data) { let uIntArray = new Uint8Array(data); if (!sourceBuffer.updating) { sourceBuffer.appendBuffer(uIntArray); } else { queue.push(data); } }); });
服务器端代码(片段)
io.on('connection', function(socket) { console.log("Client connected"); let readStream = fs.createReadStream("bunny.webm"); readStream.addListener('data', function(data) { socket.emit('video', data); }); });
我还删除了webkit检查,因为这只会在Chromium浏览器上运行.