Skip to content Skip to sidebar Skip to footer

Nodejs, Android - Video Streaming

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 downlo

Solution 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 appvar 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}));

// ConstantsvarPORT = 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 servervar 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:

Solution 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.

Post a Comment for "Nodejs, Android - Video Streaming"