在使用Node.js创建的网站上播放mp3 [英] Playing mp3 on website created with Node.js

查看:207
本文介绍了在使用Node.js创建的网站上播放mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了好几个小时和几天才能找到解决我问题的好方法.

I've searched for hours and several days for a good solution for my problem.

首先,我的场景是: 我正在使用Node.js(尤其是Sails.js)构建网站. 我们的目标是在我的服务器端目录中建立一个mp3文件列表(已经完成),并将其作为客户端(网站)上的链接.

First of all my scenario: I am building a website with Node.js - especially Sails.js. The goal is to have a list of mp3 files of my server side directory (that's already done) that are litet as links on the client side (website).

当我单击一个链接时,我希望将文件加载到我的HTML音频标签中(或者,如果有更好的针对node.js的特定解决方案(通过启动/停止控制,也可以)).

When I click on one link I want the file to be loaded in my HTML Audio Tag (or if theres a better node.js specific solution with start/stop controlling it would be fine too).

这就是我目前所在的问题...

And that's the problem where I am currently ...

正如我所说,我已经搜索了几天,以找到关于此问题的最佳或最常见的解决方案..但目前没有有用的教程或建议来解决此问题.

As I said I've searched now for days for the best or most common solution on this problem .. but at the moment there are no useful tutorials or suggestions to solve this problem.

我找到了很多模块,但是它们(如果我理解正确)仅用于在服务器端播放声音……例如:

I've found a lot of modules but they are (if I understood it right) only for playing sound on the server side ... For example: https://github.com/TooTallNate/node-speaker

最适合的解决方案可能是模块 http://binaryjs.com . 但是它说兼容性有限:

The solution that would fit the most could be the module http://binaryjs.com. But it says that it has a limited compatibility:

当前支持Chrome 15+和Firefox 11 +,IE10.后备那个 将支持Safari,移动iOS和Android,以及较旧的FF/Chrome 版本正在开发中

Currently supports Chrome 15+ and Firefox 11+, IE10. Fallbacks that will support Safari, mobile iOS and Android, and older FF/Chrome versions are in the works

所以我不确定是否应该使用它.

So I am not sure if i shoul use it.

所以我的问题: 是否有人已经为这种情况构建了类似的东西或解决方案的想法? 哪个模块最多可以完成这项工作? 还是有人对binary.js有一些经验?

So my question: Is there anyone who has already built something similar or an idea for a solution for this scenario? Which module would do this job at best? Or does anyone has some experiences with binary.js?

我希望有人能帮助我,这样我就不必再搜索几个小时了:P

I hope someone can help me so I don't have to search for hours again :P

谢谢!

推荐答案

最后,我找到了适用于我的方案的解决方案!
现在,我在服务器端读取文件,并将其转换为base64字符串.
稍后,我在客户端使用此字符串,并将其提供给我的音频标签! =)

Finally I found a solution for my scenario!
Now I read the file on the server side and convert it to a base64 string.
Later I use this string on the client side and give it to my audio tag! =)

这是我的例子.也许它也会帮助别人! :)

Here is my example. Maybe it will help someone else too! :)

这是我的DirectoryController的服务器端控制器功能:

readFile: function(req, res){
    var fs = require('fs');
    var returnData = {};

    fs.readFile('D:\\Media\\Music\\TheAudioFile.mp3', function(err, file){
        var base64File = new Buffer(file, 'binary').toString('base64');

        returnData.fileContent = base64File;

        res.json(returnData);
    });
}

现在,客户端可以通过ajax调用来调用控制器功能.就我而言,我是通过socket.io GET调用的.

function loadFile(){
    socket.get('/directory/readFile', function(response){
        var audioSrc = 'data:audio/mp3;base64,' + response.fileContent;
        var audio = new Audio();

        audio.src = audioSrc;
        audio.load();
        audio.play();
    });
}


最后,这就是整个魔术! =)

And in the end this is the whole magic! =)

如果有人有更好的解决方案==>让我知道! =)

If someone has a better solution ==> Let me know! =)

这篇关于在使用Node.js创建的网站上播放mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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