-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
89 lines (57 loc) · 1.43 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var PORT = 3000;
var Player = require( 'player' );
var http = require( 'http' );
var playingAudio = false;
var files = [
'devil_dog',
'help_me',
'manic_witches',
'monster_roar',
'terror_scream'
];
function getMp3File(){
var index = Math.floor( Math.random() * files.length );
return './mp3/' + files[ index ] + '.mp3';
}
var server = http.createServer( function( req, res ){
console.log( 'Request received for path: %s', req.url );
if( req.url === '/play/' ){
if( playingAudio ){
res.statusCode = 202;
res.end( 'Audio in progress' );
} else {
console.log( 'Starting audio...' );
var mp3File = getMp3File();
var player = new Player( mp3File );
player.on( 'error', function(){
console.log( 'Error from player!' );
playingAudio = false;
} );
player.on( 'playend', function(){
console.log( 'Audio finished playing.' );
playingAudio = false;
} );
player.play();
playingAudio = true;
res.statusCode = 200;
res.end( 'Playing audio ' + mp3File );
}
} else if( req.url === '/stop/' ){
if( playingAudio ){
console.log( 'Stopping audio...' );
player.stop();
playingAudio = false;
res.statusCode = 200;
res.end( 'Audio stopped' );
} else {
res.statusCode = 501;
res.end( 'No audio in progress' );
}
} else {
res.statusCode = 404;
res.end();
}
} );
server.listen( PORT, function(){
console.log( 'Server listening on %s', PORT );
} );