-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (103 loc) · 2.38 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
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
/**
* Module dependencies
*/
var EventEmitter = require('events');
var loadAPI = require('./lib/load-api');
var prepareEmbed = require('./lib/prepare-embed');
var sdk;
/**
* Expose `SoundCloud`
*/
module.exports = SoundCloud;
/**
* Create new `SoundCloud` controller
*
* @param {String} id of embedded widget
*/
function SoundCloud(id) {
sdk = loadAPI();
prepareEmbed(id);
this.attachToEmbed(id);
}
/**
* Mixin events
*/
SoundCloud.prototype = new EventEmitter();
/**
* Play the track.
*
* @api public
*/
SoundCloud.prototype.play = function() {
this.player.play();
};
/**
* Pause the track.
*
* @api public
*/
SoundCloud.prototype.pause = function() {
this.player.pause();
};
/**
* Remove all event handlers and free up internal player for
* garbage collection.
*
* @api public
*/
SoundCloud.prototype.destroy = function() {
this.unbindEvents();
delete this.player;
};
/**
* Attach a controller to the embedded widget
*
* @param {String} id of embedded widget
* @api private
*/
SoundCloud.prototype.attachToEmbed = function(id) {
var self = this;
sdk(function(err, SC) {
self.player = new window.SC.Widget(id);
self.bindEvents();
});
};
/**
* Bind player events
*
* @api private
*/
SoundCloud.prototype.bindEvents = function() {
var self = this;
self.player.bind(window.SC.Widget.Events.READY, function(event) {
self.emit('ready', event);
});
self.player.bind(window.SC.Widget.Events.PLAY, function(event) {
self.emit('play', event);
});
self.player.bind(window.SC.Widget.Events.PAUSE, function(event) {
self.emit('pause', event);
});
self.player.bind(window.SC.Widget.Events.FINISH, function(event) {
self.emit('end', event);
});
self.player.bind(window.SC.Widget.Events.PLAY_PROGRESS, function(event) {
self.emit('playProgress', event);
});
self.player.bind(window.SC.Widget.Events.LOAD_PROGRESS, function(event) {
self.emit('loadProgress', event);
});
};
/**
* Unbind all player events
*
* @api private
*/
SoundCloud.prototype.unbindEvents = function() {
this.player.unbind(window.SC.Widget.Events.READY);
this.player.unbind(window.SC.Widget.Events.PLAY);
this.player.unbind(window.SC.Widget.Events.PAUSE);
this.player.unbind(window.SC.Widget.Events.FINISH);
this.player.unbind(window.SC.Widget.Events.PLAY_PROGRESS);
this.player.unbind(window.SC.Widget.Events.LOAD_PROGRESS);
};