-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
76 lines (66 loc) · 1.95 KB
/
index.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
var castv2 = require('castv2-client');
var tts = require('google-tts-api');
/**
* @param {string} ip Google Home's ip address
* @param {string} [language] default: 'en'
* @param {boolean} [slow] default: false
*/
var GoogleHomePlayer = function(ip, language, slow) {
var self = this;
this.ip = ip;
this.language = language || 'en';
this.slow = slow || false;
/**
* @param {string} text a text to play
* @param {string} [language] default: this.language
* @param {boolean} [slow] default: this.slow
* @return {Promise<void>}
*/
this.say = function(text, language, slow) {
language = language || self.language;
slow = slow || self.slow;
var url = tts.getAudioUrl(text, { lang: language, slow: slow });
return self.play(url);
};
/**
* @param {string} url a sound url to play
* @param {string} [contentType] default: 'audio/mp3'
* @return {Promise<void>}
*/
this.play = function(url, contentType) {
return new Promise(function(resolve, reject) {
var client = new castv2.Client();
client.on('error', function(err) {
client.close();
reject(err);
});
client.connect(self.ip, function() {
client.launch(castv2.DefaultMediaReceiver, function(err, player) {
if (err != null) {
client.close();
reject(err);
return;
}
player.on('status', function(status) {
if (status && status.idleReason === 'FINISHED') {
client.close();
resolve();
}
});
var media = {
contentId: url,
contentType: contentType || 'audio/mp3',
streamType: 'BUFFERED'
};
player.load(media, { autoplay: true }, function(err, status) {
if (err != null) {
client.close();
reject(err);
}
});
});
});
});
};
};
module.exports = GoogleHomePlayer;