简体   繁体   English

消费使用Node.JS创建的Stream

[英]Consuming a Stream create using Node.JS

I have an application, which streams an MP3 using Node.JS. 我有一个应用程序,它使用Node.JS传输MP3。 Currently this is done through the following post route... 目前,这是通过以下发布途径完成的...

app.post('/item/listen',routes.streamFile)
...
exports.streamFile = function(req, res){
  console.log("The name is "+ req.param('name'))
  playlistProvider.streamFile(res, req.param('name'))
}
...
PlaylistProvider.prototype.streamFile = function(res, filename){
  res.contentType("audio/mpeg3");
  var readstream = gfs.createReadStream(filename, {
      "content_type": "audio/mpeg3",
      "metadata":{
        "author": "Jackie"
       },
       "chunk_size": 1024*4 });
  console.log("!")
  readstream.pipe(res);
}

Is there anyone that can help me read this on the client side? 有没有人可以帮助我在客户端阅读此书? I would like to use either JPlayer or HTML5, but am open to other options. 我想使用JPlayer或HTML5,但可以使用其他选项。

So the real problem here was, we are "requesting a file" so this would be better as a GET request. 因此,这里的真正问题是,我们正在“请求文件”,因此作为GET请求会更好。 In order to accomplish this, I used the express "RESTful" syntax '/item/listen/:name'. 为了做到这一点,我使用了表达“ RESTful”的语法“ / item / listen /:name”。 This then allows you to use the JPlayer the way specified in the links provided by the previous poster. 然后,您可以按照上一个海报提供的链接中指定的方式使用JPlayer。

I'm assuming you didn't bother visiting their site because had you done so, you would have seen several examples of how to achieve this using HTML5/JPlayer. 我假设您没有因为访问过网站而烦恼,因此,您将看到几个使用HTML5 / JPlayer实现此目的的示例。 The following is a bare-bones example provided by their online developer's documentation: 以下是其在线开发人员文档提供的基本示例:

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script> <script type="text/javascript" src="/js/jquery.jplayer.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#jquery_jplayer_1").jPlayer({ ready: function() { $(this).jPlayer("setMedia", { mp3: "http://www.jplayer.org/audio/mp3/Miaow-snip-Stirring-of-a-fool.mp3" }).jPlayer("play"); var click = document.ontouchstart === undefined ? 'click' : 'touchstart'; var kickoff = function () { $("#jquery_jplayer_1").jPlayer("play"); document.documentElement.removeEventListener(click, kickoff, true); }; document.documentElement.addEventListener(click, kickoff, true); }, loop: true, swfPath: "/js" }); }); </script> </head> <body> <div id="jquery_jplayer_1"></div> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM