I want to play a sound file on the client side whenever the space bar is pushed.
The client side successfully emits the 'bang' event when the space bar is pressed:
$(window).keydown(function(event){
alert(event.keyCode);
if (event.keyCode == 32){
socket.emit('bang');
}
});
The server side succesfully prints "bang" in the control.log so hopefully it is also emitting 'play':
io.on('connection', function(socket){
socket.on('bang', function(){
console.log('bang');
io.emit('play');
});
});
However, back on the client side to play the sound, nothing is happening. The code I am using is:
var sound = new Audio('/public/audio/1.mp3');
socket.on('play', function () {
sound.play();
});
Do I need to include something in the body to do with audio or a in order to use the Audio object.
I have searched this quite thoroughly but I am very new to node.js, html and socket.io. I tried the advice given here: Play Audio from client when message is recieved from socket.io - node.js
But when I put in:
app.use('/public', express.static('/path/to/public'));
'static' became flagged and upon trying to run the code I recieved an error message: 'express is not defined, even though I put it before:
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
as advised in that post. I also tried it after and got the same error.
Please help me.