-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3player.js
94 lines (72 loc) · 1.96 KB
/
mp3player.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
90
91
92
93
'use strict'
var fs = require('fs');
var lame = require('lame');
var Speaker = require('speaker');
var request = require('request');
var stream;
var speaker, decoder;
var url = 'https://haxmax-pillow-leeyangwoo-1.c9users.io'
var socket = require('socket.io-client')(url);
var sendIntervalID;
var sendIndex=0;
//unique device id
var mDeviceID="abcdefg"
///////////////////////////////////
//speaker = new Speaker();
// decoder = new lame.Decoder();
//stream = fs.createReadStream(data.fileurl);
// stream = fs.createReadStream("./crowd.mp3");
// stream.pipe(decoder).pipe(speaker);
/////////////////////////////////////
socket.on('connect', function(){
console.log("connected : "+url);
socket.emit('req_auth',{
DeviceID:mDeviceID
});
socket.on('res_auth',function(data) {
if(data.resultCode === "success"){
console.log("auth DEVICE : " + mDeviceID);
sendIntervalID = new setInterval(sendReport,3000);
}else{
console.log("auth failed.");
}
});
socket.on('err',function(msg){
console.log(msg);
});
socket.on('disconnect', function(){
console.log('disconnect');
clearInterval(sendIntervalID);
if (stream) {
stream.unpipe();
}
console.log('stopping')
});
});
socket.on('playcontrol', function(data) {
console.log("get file");
if (data.action == 'play') {
console.log('playing');
speaker = new Speaker();
decoder = new lame.Decoder();
//stream = fs.createReadStream(data.fileurl);
stream = request(data.fileurl);
stream.pipe(decoder).pipe(speaker);
}
if (data == 'stop') {
if (stream) {
stream.unpipe();
}
console.log('stopping')
}
})
function sendReport(){
sendIndex++;
console.log("report send : "+sendIndex);
socket.emit('report',{
Index : sendIndex,
Time:Date(),
DeviceID:mDeviceID,
content:"empty content."
});
}