forked from iobroker-community-adapters/castv2-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaPlayer.js
231 lines (177 loc) · 6.1 KB
/
mediaPlayer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"use strict"
//module initialization
module.exports = function (logClass) {
//Includes
var Client = require('./persistentClient')(logClass);
var Player = require('./persistentPlayer')(logClass);
var EventEmitter = require('events').EventEmitter;
//Default to dummyLogClass
var log = logClass ? logClass : require("./dummyLogClass")("MediaPlayer");
//MediaPlayer class
class MediaPlayer extends EventEmitter {
constructor (connection) {
super();
let that = this;
that.connection = connection;
that._name = connection.name;
//Inherit from EventEmitter
that.EVENT_CONNECTION = "connection";
that.EVENT_CLIENT_STATUS = "clientStatus";
that.EVENT_CLIENT_CONNECTED = "clientConnected";
that.EVENT_CLIENT_DISCONNECTED = "clientDisconnected";
that.EVENT_PLAYER_STATUS = "playerStatus";
that.EVENT_PLAYER_PLAYING = "playerPlaying";
that.EVENT_PLAYER_STOPPED = "playerStopped";
log.debug("New device %s at %s:%s", that.connection.name, that.connection.host, that.connection.port);
//Register for updates
that.connection.registerForUpdates(that._updateDevice.bind(that));
//Create client
that._client = new Client(that.connection);
//Forward some client events
that._client.on(that._client.EVENT_STATUS, that.emit.bind(that, that.EVENT_CLIENT_STATUS));
that._client.on(that._client.EVENT_CONNECTED, that.emit.bind(that, that.EVENT_CLIENT_CONNECTED));
that._client.on(that._client.EVENT_DISCONNECTED, that.emit.bind(that, that.EVENT_CLIENT_DISCONNECTED));
//Create player
that._player = new Player(that._name, that._client);
//Forward some player events
that._player.on(that._player.EVENT_STATUS, that.emit.bind(that, that.EVENT_PLAYER_STATUS));
that._player.on(that._player.EVENT_PLAYING, that.emit.bind(that, that.EVENT_PLAYER_PLAYING));
that._player.on(that._player.EVENT_STOPPED, that.emit.bind(that, that.EVENT_PLAYER_STOPPED));
}
/*
* Public MediaPlayer methods
*/
//playURL()
playUrlPromise (url, options) {
return this._player.playUrl(url, options);
}
//CLIENT ACTIONS
//getVolume
getVolume() {
return this._client.getVolume();
}
//isMuted
isMuted() {
return this._client.getVolume().muted;
}
//getVolumePromise
getVolumePromise() {
return this._client.getVolumePromise();
}
//setVolumePromise
setVolumePromise(volume) {
return this._client.setVolumePromise(volume);
}
//mutePromise
mutePromise(volume) {
return this._client.mutePromise();
}
//unmutePromise
unmutePromise(volume) {
return this._client.unmutePromise();
}
//stopPromise
stopClientPromise() {
return this._client.stopPromise();
}
//getClientStatus <- returns cache
getClientStatus() {
return this._client.getStatus();
}
//getPreviousClientStatus <- returns cache
getPreviousClientStatus() {
return this._client.getPreviousStatus();
}
//PLAYER ACTIONS
//pausePromise
pausePromise() {
return this._player.pausePromise();
}
//playPromise
playPromise() {
return this._player.playPromise();
}
//stopPromise
stopPromise() {
return this._player.stopPromise();
}
//seekPromise
seekPromise(currentTime) {
return this._player.seekPromise(currentTime);
}
getStatusPromise() {
return this._player.getStatusPromise
}
//getPlayerStatus <- returns cache
getPlayerStatus() {
return this._player.getStatus();
}
//getPreviousPlayerStatus <- returns cache
getPreviousPlayerStatus() {
return this._player.getPreviousStatus();
}
//PLAYLIST ACTIONS
//get current playlist index
getCurrentPlaylistIndex() {
return this._player.getCurrentPlaylistIndex();
}
//get current playlist itemID
getCurrentPlaylistId() {
return this._player.getCurrentPlaylistId();
}
//get full playlist
getPlaylist(id) {
return this._player.getPlaylist(id);
}
//get playlist item with itemID
getPlaylistItemWithId(id) {
return this._player.getPlaylistItemWithId(id);
}
//get playlist item with playlist index
getPlaylistItemWithIndex(index) {
return this._player.getPlaylistItemWithIndex(index);
}
//update playlist
updatePlaylistPromise(items, options) {
return this._player.updatePlaylistPromise(items, options);
}
//inser into playlist
insertIntoPlaylistPromise(items, options) {
return this._player.insertIntoPlaylistPromise(items, options);
}
//remove from playlist
removeFromPlaylistPromise(itemIds, options) {
return this._player.removeFromPlaylistPromise(itemIds, options);
}
//reorder playlist
reorderPlaylistPromise(itemIds, options) {
return this._player.reorderPlaylistPromise(itemIds, options);
}
//jump in playlist
jumpInPlaylistPromise(jump) {
return this._player.jumpInPlaylistPromise(jump);
}
//set repeatMode - REPEAT_OFF, REPEAT_ALL, REPEAT_SINGLE, REPEAT_ALL_AND_SHUFFLE
setRepeatModePromise(repeatMode) {
return this._player.setRepeatModePromise(repeatMode);
}
//playAnnouncementPromise
playAnnouncementPromise (url, options) {
return this._player.playAnnouncementPromise (url, options);
}
/*
* Private methods
*/
//update device
_updateDevice (device) {
let that = this;
that.device = device;
log.debug("Updated device %s at %s:%s - will reconnect", that.connection.name, that.connection.host, that.connection.port);
that._client.updateDevice(device);
//Emit new connection
that.emit(that.EVENT_CONNECTION, device);
}
}
//Export MediaPlayer class
return MediaPlayer;
}