Permalink
Browse files

Stream de audio

  • Loading branch information...
1 parent 23bb5e2 commit fda8dffc5ff583db49daf37f1f84410c6721da97 @emerleite committed May 17, 2011
Showing with 40 additions and 0 deletions.
  1. +40 −0 audio.js
  2. BIN musica.mp3
View
@@ -0,0 +1,40 @@
+var http = require('http'),
+ fileSystem = require('fs'),
+ path = require('path'),
+ util = require('util');
+
+var server = http.createServer(function(request, response) {
+ var filePath = path.join(__dirname, 'musica.mp3');
+ var stat = fileSystem.statSync(filePath);
+
+ var inicio = 0;
+ var fim = 0;
+
+ var range = request.headers.range;
+ console.log(range);
+
+ if (range) {
+ inicio = parseInt(range.slice(range.indexOf("bytes=") + 6, range.indexOf("-")));
+ fim = parseInt(range.slice(range.indexOf("-") + 1, range.length));
+ }
+
+ if (isNaN(fim) || fim == 0) {
+ fim = stat.size-1;
+ }
+
+ if (inicio > fim) return;
+
+ response.writeHead(206, {
+ 'Content-Type': 'audio/mpeg',
+ 'Content-Length': stat.size,
+ 'Content-Range':'bytes '+ inicio + '-' + fim + '/' + stat.size,
+ 'Transfer-Encoding':'chunked'
+ });
+
+ var readStream = fileSystem.createReadStream(filePath, { flags: 'r', start: inicio, end: fim});
+
+ util.pump(readStream, response);
+});
+
+server.listen(2000);
+
View
Binary file not shown.

0 comments on commit fda8dff

Please sign in to comment.