Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

How to play websocket video data by Javascript and Html5?

I'm building a live system. Now I want to make a player that gets video data(raw) from the server and show them via <video> to my customers.

My code goes like this:

var ws=new WebSocket("wss://******");
var Ms=new MediaSource();
var mime='video/webm; codecs="vorbis,vp8"';
var rec=0; var que=[];
var video=document.getElementById("vid1");
video.src=URL.createObjectURL(Ms);
Ms.addEventListener('sourceopen',sourceOpen);
function sourceOpen(ee){
        URL.revokeObjectURL(video.src);
        var Ms=ee.target;
        var buffer=Ms.addSourceBuffer(mime);
        ws.onmessage=function(e){
            buffer.appendBuffer(e.data);
            buffer.addEventListener('updateend',function(e){
                if(!buffer.updating&&Ms.readyState==='open'){
                        Ms.endOfStream(); video.play();
                }
            });
        }
ws.binaryType='arraybuffer';

Notice: i'm not sure if i have written the correct code.

Now, i can get only blank from the video, Google Chrome doesn't throw out errors either.

New progress: The code works well in Firefox, but Chrome still showed blank T_T Please help.

question from:https://stackoverflow.com/questions/66062430/how-to-play-websocket-video-data-by-javascript-and-html5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...