File tree 1 file changed +61
-2
lines changed
1 file changed +61
-2
lines changed Original file line number Diff line number Diff line change 1
- //api - https://coderadio-admin.freecodecamp.org/api/live/nowplaying/coderadio
1
+ //Api
2
+ const api = 'https://coderadio-admin.freecodecamp.org/api/live/nowplaying/coderadio' ;
2
3
3
- const api = 'https://coderadio-admin.freecodecamp.org/api/live/nowplaying/coderadio' ;
4
+ //DOM Elements
5
+ const songTitleElement = document . querySelector ( '.track-title' ) ;
6
+ const songArtistElement = document . querySelector ( '.track-artist' ) ;
7
+ const playerElement = document . getElementById ( 'player' ) ;
8
+ const selectBitrateElement = document . getElementById ( 'select-bitrate' ) ;
9
+
10
+ const song = {
11
+ bitrate : { } ,
12
+ url : { }
13
+ } ;
14
+
15
+ function getSong ( ) {
16
+ fetch ( api )
17
+ . then ( response => {
18
+ return response . json ( ) ;
19
+ } )
20
+ . then ( data => {
21
+ console . log ( data ) ;
22
+ song . title = data . now_playing . song . title ;
23
+ song . artist = data . now_playing . song . artist ;
24
+
25
+ song . bitrate . high = data . station . mounts [ 0 ] . bitrate ;
26
+ song . url . high = data . station . mounts [ 0 ] . url ;
27
+
28
+ song . bitrate . low = data . station . mounts [ 1 ] . bitrate ;
29
+ song . url . low = data . station . mounts [ 1 ] . url ;
30
+ } )
31
+ . then ( ( ) => {
32
+ displayInfo ( ) ;
33
+ } )
34
+ . catch ( error => {
35
+ songTitleElement . innerText = error . message ;
36
+ } ) ;
37
+ }
38
+
39
+ function displayInfo ( ) {
40
+ songTitleElement . innerText = song . title ;
41
+ songArtistElement . innerText = song . artist ;
42
+ let link = changeBitrate ( ) ;
43
+ if ( playerElement . getAttribute ( 'src' ) != link ) {
44
+ playerElement . pause ( ) ;
45
+ playerElement . setAttribute ( 'src' , link ) ;
46
+ playerElement . load ( ) ;
47
+ playerElement . play ( ) ;
48
+ }
49
+ }
50
+
51
+ function changeBitrate ( ) {
52
+ switch ( selectBitrateElement . value ) {
53
+ case 'normal-128' :
54
+ return song . url . high ;
55
+ case 'normal-64' :
56
+ return song . url . low ;
57
+ default :
58
+ return ;
59
+ }
60
+ }
61
+ getSong ( ) ;
62
+ selectBitrateElement . addEventListener ( 'change' , getSong ) ;
You can’t perform that action at this time.
0 commit comments