Nodejs,Android-视频流 [英] Nodejs, Android - Video Streaming
问题描述
我正在开发一个显示来自Nodejs& amp;的视频的应用程序MongoDB服务器.
I am working on an application which displays videos from Nodejs& MongoDB server.
这里的问题是因为视频没有流传输,Android上的mediaPlayer会完全下载视频,然后显示它,这确实很慢,您可以猜到.
The problem here is because the videos are not streaming, mediaPlayer on Android fully downloads video and then displays it and this is really slow as you can guess.
我不是母语人士,因此阅读文档对我来说很难理解.
I am not a native speaker so reading documents mostly hard to understand for me.
我应该走哪条路? RTSP或HTTP流.有什么技巧可以实现我的目标吗?
Which path should I go? RTSP or HTTP streaming. Any tips how can I achieve my goal?
衷心感谢您的帮助.
推荐答案
虽然专用的流服务器对于任何大型解决方案或任何需要良好性能的解决方案可能都是更好的解决方案,但您绝对应该能够从标准格式中流式传输视频文件. node.js应用程序.
While dedicated streaming servers are probably a better solution for any sizeable solution, or any solution requiring good performance, you definitely should be able to stream video files from a standard node.js application.
最简单的方法是将视频放置在服务器上的某个目录中,并将其作为静态内容提供.
The simplest way is to place the videos in a directory somewhere on the server and serve them as static content.
以下非常基本的node.js应用将提供视频-您可以从基本url访问视频,然后输入目录和视频文件名-例如
The following very basic node,js app will serve video - you access it from your base url followed by the directory and video file name - e.g. http://[your server url]/videos/[name of your video file]
var express = require('express');
var fs = require('fs');
var morgan = require('morgan');
//Define the app
var app = express();
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'});
// setup the logger
app.use(morgan('combined', {stream: accessLogStream}));
// Constants
var PORT = 3000;
//Use static middleware to serve static content - using absolute paths here (you may want something different)
app.use('/videos', express.static('/videos'));
//Add error handling
app.use(function(err, req, res, next) {
console.log("error!!!");
console.log(err.stack);
});
// Video Server test page
app.get('/', function (req, res) {
console.log("In Get!!!");
res.send('Hello world from Video server\n');
});
//Start web server
var server = app.listen(PORT, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
简单HTTP流可能对您不起作用的一个原因是,如果您的服务器不支持范围请求.这种机制允许客户端一次只请求文件的一部分.有关更多信息,请参见此处:
One reason why simple HTTP streaming might not be working for you is if your server does not support range request. This mechanism allows the client request just a portion of the file at a time. See here for more info:
这篇关于Nodejs,Android-视频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!