[英]MediaSource API: Video not playing
我正在为运行在RPi上的Magic Mirror创建模块。 该模块应该允许用户在手机上选择视频文件,开始读取文件,并将流发送回魔术镜上的html video标签。 这更像是将视频从移动设备镜像/广播到魔术镜(rpi)。 该框架基于Nodejs。
目前,我正在尝试读取本地文件,并将流发送到客户端。
我正在为服务器使用以下代码:
module.exports = NodeHelper.create({
socketNotificationReceived: function(notification, payload) {
var self = this;
switch(notification) {
case "INITIATEDEVICES":
var readStream = fs.createReadStream("./modules/MMM-MP4Player/video.mp4");
readStream.addListener('data', function(data){
self.sendSocketNotification('Video_File',data);
});
break;
}
}
});
以下代码适用于客户端:
Module.register("MMM-MP4Player",{
start: function(){
window.URL = window.URL || window.webkitURL;
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if(!!! window.MediaSource){
console.log('MediaSource API is not available!');
}
},
getDom: function() {
var self = this;
wrapper = document.createElement("div");
videoElement = document.createElement("video");
videoElement.width = 1280;
videoElement.height = 720;
videoElement.controls = true;
videoElement.autoplay = true;
videoElement.id = self.identifier+"_player";
wrapper.appendChild(videoElement);
setTimeout(function(){
self.mediaSource = new MediaSource();
self.queue = [];
videoElement.src = window.URL.createObjectURL(self.mediaSource);
self.mediaSource.addEventListener('sourceopen', function(e){
self.sourceBuffer = self.mediaSource.addSourceBuffer('video/mp4; codecs="avc1.42E01E,mp4a.40.2"'); // video/webm; codecs="vorbis,vp9"
videoElement.play();
self.sourceBuffer.addEventListener('update', function() {
if (self.queue.length > 0 && !self.sourceBuffer.updating) {
self.sourceBuffer.appendBuffer(self.queue.shift());
}
});
}, false);
self.sendSocketNotification("INITIATEDEVICES");
}, 2000);
return wrapper;
},
socketNotificationReceived: function(notification, payload){
var self = this;
switch(notification){
case "Error": // Universal error handler
break;
case "Video_File":
if (self.sourceBuffer.updating || self.queue.length > 0) {
self.queue.push(new Uint8Array(payload.data));
} else {
self.sourceBuffer.appendBuffer(new Uint8Array(payload.data));
}
break;
}
}
});
视频块可以完美地从服务器发送,也可以由客户端接收。 只是视频播放器保持空白。 欢迎所有建议。
谢谢。
好的。 因此,在没有stackoverflow帮助的情况下,我继续尝试并最终纠正了该问题。 张贴在这里只是为了帮助别人。
我对代码做了一些小的调整,并将文件更改为与破折号兼容的文件。 现在需要关注如何将视频缓冲区实时转换为符合破折号的规则。 无论如何,这里是代码。
Module.register("MMM-MP4Player",{
getDom: function() {
var self = this;
wrapper = document.createElement("div");
videoElement = document.createElement("video");
videoElement.width = 300;
videoElement.height = 200;
videoElement.controls = true;
videoElement.autoplay = true;
videoElement.id = self.identifier+"_player";
wrapper.appendChild(videoElement);
window.URL = window.URL || window.webkitURL;
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
self.mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
self.queue = [];
if(window.MediaSource && window.MediaSource.isTypeSupported(self.mimeCodec)){
setTimeout(function(){
self.mediaSource = new MediaSource();
videoElement.src = window.URL.createObjectURL(self.mediaSource);
videoElement.play();
self.mediaSource.addEventListener('sourceopen', function(e){
self.sourceBuffer = self.mediaSource.addSourceBuffer(self.mimeCodec);
self.sourceBuffer.addEventListener('updateend', function() {
if (self.queue.length > 0 && !self.sourceBuffer.updating) {
self.sourceBuffer.appendBuffer(self.queue.shift());
}
}, false);
}, false);
self.sendSocketNotification("INITIATEDEVICES");
}, 2000);
}
return wrapper;
},
socketNotificationReceived: function(notification, payload){
var self = this;
switch(notification){
case "Video_File":
if (self.sourceBuffer.updating || self.queue.length > 0) {
self.queue.push(new Uint8Array(payload));
} else {
self.sourceBuffer.appendBuffer(new Uint8Array(payload));
}
break;
}
}
});
欢迎在Node.js中提供有关视频缓冲区转换的任何帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.