[英]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.