-
Notifications
You must be signed in to change notification settings - Fork 3
/
OhMyRadio.js
341 lines (310 loc) · 9.92 KB
/
OhMyRadio.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/** @license
*
* OhMyRadio.js - An Online Radio Player
*
* Essentially requires SoundManager2 Library:
* http://schillmania.com/projects/soundmanager2/
*
* ----------------------------------------------
* Web: https://github.com/vishaltelangre/OhMyRadio
*
* Copyright (c) 2013, Vishal Telangre. All rights reserved.
* Code provided under the MIT License:
* https://github.com/vishaltelangre/OhMyRadio/LICENSE.md
*
* Version 0.0.1
*/
(function(window, undefined) {
/*
* ------------------------
* Global Variable Setup
* ------------------------
*/
var sm = soundManager,
PLAYING = 0,
PAUSED = 1,
STOPPED = 2;
/*
* ------------------------
* Private
* ------------------------
*/
var _local = {
settings: {}, /* _local.settings */
defaults: {
stations: {
Marathi_Spacia_Net: "http://208.80.53.106:16704",
Hindi_Evergreen: "http://50.7.77.114:8296",
Nonstop_Hindi: "http://159.253.145.180:7090",
Radio_Gabbar: "http://50.7.77.114:8296",
CENEMIX: "http://192.184.9.79:8006",
Streaming_Soundtracks: "http://91.121.140.11:8000",
SoundTrax_FM: "http://91.121.140.11:8000",
Black_Beats_Radio: "http://93.119.227.84:9000",
Radio_DEEA: "http://178.157.81.147:8090",
COOL_fahrenheit_93: "http://5.231.68.21:8004",
Radio_Sound_Popup_Brasil: "http://173.193.202.68:7460",
Rock_Radio: "http://174.36.206.197:8000",
FM_181_Highway: "http://108.61.73.118:14018",
FM_181_Buzz: "http://108.61.73.120:14126",
Chart_Hits_Top_40: "http://95.141.24.98:80",
Americas_Best_Ballads: "http://108.61.73.118:14018",
Piano_N: "http://222.122.131.58:6400",
Kick_Radio_80s: "http://173.193.202.87:8085",
Planet_Radio_80s: "http://67.213.220.36:80"
} /* _local.defaults.stations */
}, /* _local.defaults */
playerState: null, /* _local.playerState */
lastUpdatedAt: 0, /* _local.lastUpdatedAt */
playlist: {}, /* _local.playlist */
/*
* Usage:
* each([1,2,3], function(i){})
*/
each: function(a, b) {
var d = a.length,
e = Array.prototype.forEach;
if (isNaN(d))
for (var f in a)
c.isOwn(a, f) && b(a[f], f, a);
else if (e)
e.call(a, b);
else
for (var g = 0; d > g; g++)
b(a[g], g, a);
},/* _local.each */
/*
* Usage:
* target = extend({}, toObj, fromObj )
*/
extend: function(copy, to, from){
this.each(Array.prototype.slice.call(arguments, 1), function(objcts){
var loop = function(final, obj){
for( prop in obj ){
if( typeof obj[prop] !== "object" ){
final[prop] = obj[prop];
}
else{
if(typeof final[prop] === "undefined") final[prop] = {};
loop(final[prop], obj[prop]);
}
}
}
loop(copy, objcts);
});
return copy;
}, /* _local.extend */
/*
* Logs a message for the developer if logging is on.
*/
log: function() {
if (true) {
var time = new Date().toLocaleTimeString();
var LOG_PREFIX = "[OhMyRadio] - " + time;
var args = Array.prototype.slice.call( arguments );
args.unshift( LOG_PREFIX + " ::" );
window.console &&
( window.console.log.apply
? window.console.log.apply( window.console, args )
: window.console.log( Array.prototype.slice.call( args, 0 ).join(" "))
)
}
}, /* _local.log */
/*
* Event listeners
*/
events: {
play: function(e) {
_local.playerState = PLAYING;
},
stop: function(e) {
_local.playerState = STOPPED;
},
pause: function(e) {
_local.playerState = PAUSED;
},
resume: function(e) {
_local.playerState = PLAYING;
},
finish: function(e) {
_local.log("Nothing to play now!");
},
whileloading: function(e) {
},
onload: function(e) {
if (_local.sound.readyState == 2) {
}
},
whileplaying: function(e) {
var d = new Date();
if (d - _local.lastUpdatedAt > 50){
_local.lastUpdatedAt = d;
}
},
ontimeout: function(){
_local.log("Timed out! Try again.");
},
id3: function(id3){
_local.log("ID3 Info: ", id3);
}
}, /* _local.events */
/*
* Keeps currently playing station's soundManager sound object's copy here.
* We'll use this everywhere to manage currently playing station.
*/
sound: {}, /* _local.sound */
/*
* Creates soundManager sound object and cache it in the `_local.playlist`
* store. If the sound object is already exist in the store, then it is
* returned without creating a new object.
*/
prepareStation: function( stationName, stationMeta ) {
var currentStation;
if ( _local.playlist[ stationName ] ){
currentStation = _local.playlist[ stationName ];
}
else {
currentStation = sm.createSound({
id: stationName,
url: stationMeta[ stationName ] + "/;OhMyRadio",
onplay: _local.events.play,
onstop: _local.events.stop,
onpause: _local.events.pause,
onresume: _local.events.resume,
onfinish: _local.events.finish,
whileloading: _local.events.whileloading,
whileplaying: _local.events.whileplaying,
onid3: _local.events.id3,
onload: _local.events.onload,
ontimeout: _local.events.ontimeout,
autoPlay: false,
type: 'audio/mp3',
volume: _local.settings.volume || 100
});
_local.playlist[ stationName ] = currentStation;
}
_local.log("Playing:", stationName);
return currentStation;
}, /* _local.prepareStation */
}; /* _local */
/*
* ------------------------
* Public
* ------------------------
*/
var _public = {
togglePlay: function(){
switch( _local.playerState ) {
case PLAYING:
_local.sound.pause();
break;
case PAUSED:
_local.sound.resume();
break;
case STOPPED:
_local.sound.play();
break;
default:
_local.log("Gotta fucked up somewhere! :(");
}
return _local.playerState;
}, /* _public.togglePlay */
/*
* Increase the volume of sound on a scale of 0-100.
*/
increaseVolume: function( increaseBy ){
var increaseBy = increaseBy || 10;
if ( _local.sound.volume <= 100 - increaseBy ){
_local.sound.setVolume( _local.sound.volume + increaseBy );
}
return _local.sound.volume;
}, /* _public.increaseVolume */
/*
* Decrease the volume of sound on a scale of 0-100.
*/
decreaseVolume: function( decreaseBy ){
var decreaseBy = decreaseBy || 10;
if ( _local.sound.volume >= decreaseBy ){
_local.sound.setVolume( _local.sound.volume - decreaseBy );
}
return _local.sound.volume;
}, /* _public.decreaseVolume */
/*
* Play the station by name from the `stations` list.
*/
playByName: function( name ) {
var stationMeta = {};
stationMeta[ name ] = _local.settings.stations[ name ];
// stop currently playing station before proceeding
if ( _local.sound.sID ) {
_local.sound.stop();
_local.sound.unload();
}
if( stationMeta === "undefined" ) {
_local.log("Can't find station:" + name);
return;
}
_local.playlist.currentlyPlaying = stationMeta;
_local.sound = _local.prepareStation( name, stationMeta );
_local.sound.play();
// return _local.sound;
return stationMeta;
}, /* _public.playByName */
/*
* Play the station from the `stations` list by id.
*/
playById: function( id /* {Integer} */ ) {
// sbottom currently playing station before proceeding
if ( _local.sound.sID ) {
_local.sound.stop();
_local.sound.unload();
}
var stationName = Object.keys( _local.settings.stations )[ id ];
if( stationName === "undefined" ) {
_local.log("Can't find such station!");
return;
}
return _public.playByName( stationName );
}, /* _public.playById */
/*
* Plays the next station from the available `stations` list.
*/
nextStation: function() {
var nextId,
allStations = _local.settings.stations,
currentlyPlayingMeta = _local.playlist.currentlyPlaying,
currentlyPlayingName = Object.keys( currentlyPlayingMeta )[0],
currentlyPlayingId = Object.keys( allStations ).indexOf( currentlyPlayingName );
if ( currentlyPlayingId + 1 >= Object.keys( allStations ).length ) {
nextId = 0;
} else {
nextId = currentlyPlayingId + 1;
}
return _public.playById( nextId );
}, /* _public.nextStation */
/*
* Plays the previous station from the available `stations` list.
*/
prevStation: function() {
var prevId,
allStations = _local.settings.stations,
currentlyPlayingMeta = _local.playlist.currentlyPlaying,
currentlyPlayingName = Object.keys( currentlyPlayingMeta )[0],
currentlyPlayingId = Object.keys( allStations ).indexOf( currentlyPlayingName );
if ( currentlyPlayingId - 1 < 0 ) {
prevId = Object.keys( allStations ).length - 1;
} else {
prevId = currentlyPlayingId - 1;
}
return _public.playById( prevId );
}, /* _public.nextStation */
init: function( config ){
if ( typeof config !== "object" ) config = {};
_local.extend( _local.settings, _local.defaults, config );
if ( sm.flashVersion >= 9 )
sm.defaultOptions.usePeakData = _local.settings.usePeakData;
return _public.playById( 0 );
} /* _public.init */
}; /* _public */
window.OhMyRadio = _public;
}(window));