简体   繁体   English

播放音频到套接字node.js

[英]Playing audio to sockets node.js

I have a node.js app which should plays 3 sounds to all of the sockets at a certain time with node.js. 我有一个node.js应用程序,该应用程序应该在某个时间使用node.js播放所有插座的3种声音。

How would I go about getting this to work? 我将如何开始工作呢? Should I have the sound clips on the app.js and somehow emit them? 我应该在app.js上放上声音片段,然后以某种方式发出声音片段吗? Or should I have them stored in the client Side HTML and a flag to play them emitted to the users? 还是应该将它们存储在客户端HTML中并标记一个播放它们的标志发送给用户?

Could I see examples of how to do the prefered method also? 我还能看到一些如何做首选方法的例子吗? Thank you 谢谢

I suggest you to make the 3 audio files public (for example /public/audio/1.mp3 and relative node routes) and then to split the problem in 2 sub-problems: 我建议您公开3个音频文件(例如/public/audio/1.mp3和相对节点路由),然后将问题分为2个子问题:

  • The first is HOW to play the audio in JavaScript? 首先是如何以JavaScript播放音频? example
  • The second is WHEN to play the audio? 第二个是何时播放音频?

For example if you're using socket.io I suggest you this simple code (server side): 例如,如果您使用的是socket.io,我建议您使用以下简单代码(服务器端):

io.emit('play', { audio: '/public/audio/1.mp3'});

And, for example, this can be the client side code needed: 并且,例如,这可能是所需的客户端代码:

var socket = io.connect('/');
socket.on('play', function (data) {
    // Here goes the 'HOW' piece of code
    // to play the data.audio track
});

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

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