我上传了很多音频记录到我的服务器。每条记录的持续时间约为45分钟。我想在我的网站上播放这些文件,如youtube,但仅限于音频格式。我正在使用Web音频API,我听说过html5本地存储。使用这个html本地存储或直接从服务器存储播放文件更好吗? 如何从存储我的网站的服务器上的绝对路径播放文件?可以使用xmlHttpRequest或什么来完成?
答案 0 :(得分:0)
这个功能齐全的nodejs服务器可能会给你带来一些牵引力......在服务器上保存以下代码作为文件media_server.js
// usage
// node this_file.js
var http = require('http'),
fs = require('fs'),
util = require('util');
// var path = "/path/to/audio/or/video/file/local/to/server/cool.mp4"; // put any audio or video file here
var path = "/home/stens/Dropbox/Documents/data/audio/Justice_Genesis_first_10_seconds_june.flac";
var port = 8888;
var host = "localhost"; // update if using a real server not your laptop
http.createServer(function (req, res) {
var stat = fs.statSync(path);
var total = stat.size;
if (req.headers.range) { // meaning client (browser) has moved the forward/back slider
// which has sent this request back to this server logic ... cool
var range = req.headers.range;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total-1;
var chunksize = (end-start)+1;
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);
var file = fs.createReadStream(path, {start: start, end: end});
res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/mp4' });
file.pipe(res);
} else {
console.log('ALL: ' + total);
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' }); // muck with making this dynamically match source media type
fs.createReadStream(path).pipe(res);
}
}).listen(port, host);
console.log("Server running at http://" + host + ":" + port + "/");
然后通过在服务器端发出nodejs调用来执行它(你的笔记本电脑在这里工作得很好; - )
node media_server.js
这将启动一个服务器,监听所选端口8888 ...现在只需将浏览器指向该主机和端口,按照URL
它会将媒体流式传输(不下载)到您的浏览器上并呈现音频或视频......享受