问题
I am working on an application which displays videos from Nodejs& MongoDB server.
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.
Which path should I go? RTSP or HTTP streaming. Any tips how can I achieve my goal?
All your help is appreciated, best regards.
回答1:
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.
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);
});
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:
- https://en.wikipedia.org/wiki/Byte_serving
回答2:
You have not given details about your application. RTSP or HLS are streaming formats having some advantages over other as per requirement, and Android supports both and more as well.
来源:https://stackoverflow.com/questions/38909393/nodejs-android-video-streaming