0

I'm using node.js and torrent-stream to create a readable stream of a downloading audio file. I've piped this into an audio file in my directory.

My question is, how would I stream this audio file in node.js. Is there some standard way to play a file as it downloads?

asked Feb 20, 2017 at 1:23

2 Answers 2

3

I don't know of a way to play an audio file as it is downloaded, but here is some code that might work for what you are trying to do:

var http = require('http'),
 fs = require('fs'),
 filePath = 'myfile.mp3',
 stat = fs.statSync(filePath);
http.createServer(function(request, response) {
 response.writeHead(200, {
 'Content-Type': 'audio/mpeg',
 'Content-Length': stat.size
 });
 fs.createReadStream(filePath).pipe(response);
})
.listen(2000);

Hope this helps!

answered Feb 20, 2017 at 15:14
Sign up to request clarification or add additional context in comments.

Comments

1

I tried this out and it seems to work:

const http = require('http')
const util = require('util')
const mime = require('mime')
const PORT = process.env.PORT || 8080
const onFinished = require('on-finished')
const parseRange = require('range-parser')
const torrentStream = require('torrent-stream')
const engine = torrentStream('magnet:?xt=urn:btih:b42f85d4d976f31d9edde30a101c79928e1353f6')
engine.on('ready', (files) => {
 http.createServer(function (req, res) {
 const index = parseInt(req.url.slice(1), 10)
 if (!files.hasOwnProperty(index)) return res.end()
 const file = files[index || 0]
 console.log(`${file.name} ${req.headers.range ? req.headers.range : ''}`)
 send(req, res, file)
 }).listen(PORT, function () { console.log('Server listening on: http://localhost:%s', PORT) })
})
function send (req, res, file) {
 let stream
 let len = file.length
 const type = mime.lookup(file.path)
 if (type) {
 const charset = mime.charsets.lookup(type)
 res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''))
 }
 res.setHeader('Content-Length', len)
 res.setHeader('Accept-Ranges', 'bytes')
 if (req.headers.range) {
 const ranges = parseRange(len, req.headers.range, { combine: true })
 if (ranges === -1) {
 res.setHeader('Content-Length', 0)
 res.setHeader('Content-Range', contentRange('bytes', len))
 res.statusCode = 416
 return res.end()
 }
 if (ranges !== -2 && ranges.length === 1) {
 res.statusCode = 206
 res.setHeader('Content-Range', contentRange('bytes', len, ranges[0]))
 len = ranges[0].end - ranges[0].start + 1
 res.setHeader('Content-Length', len)
 if (req.method === 'HEAD') return res.end()
 stream = file.createReadStream(ranges[0])
 }
 } else {
 if (req.method === 'HEAD') return res.end()
 stream = file.createReadStream()
 }
 onFinished(res, () => stream && stream.destroy())
 stream.pipe(res)
 return stream
}
function contentRange (type, size, range) {
 return type + ' ' + (range ? range.start + '-' + range.end : '*') + '/' + size
}
answered Feb 20, 2017 at 18:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.