Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有快进/快退功能的流式mp3文件快速服务器_Javascript_Node.js_Express_Filesystems - Fatal编程技术网

Javascript 具有快进/快退功能的流式mp3文件快速服务器

Javascript 具有快进/快退功能的流式mp3文件快速服务器,javascript,node.js,express,filesystems,Javascript,Node.js,Express,Filesystems,我有一个小型express服务器,可以下载或流式传输mp3文件,如下所示: 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.htm

我有一个小型express服务器,可以下载或流式传输mp3文件,如下所示:

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:


您的浏览器不支持音频元素。
这是可行的,但是,音频流不会倒带或快进。我是否必须更改请求头中的某些内容才能允许这种情况发生?也许我需要设置范围并添加开始和结束时间或其他内容。任何小费都将不胜感激。谢谢。

找到了答案


path\index.js:25 const parts=range.replace(/bytes=/,'').split('-');^TypeError:无法读取FSReqWrap.oncomplete(fs.js:141:20)的suppressedCallback(fs.js:200:5)中fs.exists(path\index.js:25:33)处未定义的属性“replace”我明白了error@MohamedFarouk不确定为什么没有定义范围。查看标题中的内容并验证范围是否存在。
<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>
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!'));