admin管理员组文章数量:1327976
I have a little express server that either downloads or streams an mp3 file, which looks like this:
const express = require('express');
const fs = require('fs');
const app = express();
app.use('/mp3', express.static(__dirname + '/mp3'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/stream', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
fs.exists(file, (exists) => {
if (exists) {
const rstream = fs.createReadStream(file);
rstream.pipe(res);
} else {
res.send('Error - 404');
res.end();
}
});
});
app.get('/download', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
res.download(file);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
html:
<audio controls="controls">
<source src="http://localhost:3000/stream" type="audio/ogg" />
<source src="http://localhost:3000/stream" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
This works, however, the audio stream does not rewind or fast forward. Do I have to change something in the request headers to allow this to happen? Perhaps I need to set ranges and add start and end times or something. Any tip would be appreciated. Thank you.
I have a little express server that either downloads or streams an mp3 file, which looks like this:
const express = require('express');
const fs = require('fs');
const app = express();
app.use('/mp3', express.static(__dirname + '/mp3'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/stream', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
fs.exists(file, (exists) => {
if (exists) {
const rstream = fs.createReadStream(file);
rstream.pipe(res);
} else {
res.send('Error - 404');
res.end();
}
});
});
app.get('/download', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
res.download(file);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
html:
<audio controls="controls">
<source src="http://localhost:3000/stream" type="audio/ogg" />
<source src="http://localhost:3000/stream" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
This works, however, the audio stream does not rewind or fast forward. Do I have to change something in the request headers to allow this to happen? Perhaps I need to set ranges and add start and end times or something. Any tip would be appreciated. Thank you.
Share Improve this question asked Mar 28, 2018 at 0:17 inertiainertia 4,1272 gold badges20 silver badges26 bronze badges1 Answer
Reset to default 6Found the answer here.
const express = require('express'),
bodyParser = require('body-parser'),
path = require('path'),
fs = require('fs'),
app = express();
// app.use('/mp3', express.static(__dirname + '/mp3'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/stream', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
const stat = fs.statSync(file);
const total = stat.size;
if (req.headers.range) {
}
fs.exists(file, (exists) => {
if (exists) {
const range = req.headers.range;
const parts = range.replace(/bytes=/, '').split('-');
const partialStart = parts[0];
const partialEnd = parts[1];
const start = parseInt(partialStart, 10);
const end = partialEnd ? parseInt(partialEnd, 10) : total - 1;
const chunksize = (end - start) + 1;
const rstream = fs.createReadStream(file, {start: start, end: end});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes', 'Content-Length': chunksize,
'Content-Type': 'audio/mpeg'
});
rstream.pipe(res);
} else {
res.send('Error - 404');
res.end();
// res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'audio/mpeg' });
// fs.createReadStream(path).pipe(res);
}
});
});
app.get('/download', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
res.download(file);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
本文标签: javascriptstream mp3 file express server with ability to fast forwardrewindStack Overflow
版权声明:本文标题:javascript - stream mp3 file express server with ability to fast forwardrewind - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251047a2440765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论