流式mp3文件快速服务器具有快进/快退功能 [英] stream mp3 file express server with ability to fast forward/rewind

查看:189
本文介绍了流式mp3文件快速服务器具有快进/快退功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

找到答案这里

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!'));

这篇关于流式mp3文件快速服务器具有快进/快退功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆