Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Radio {
  2.   constructor (stations) {
  3.     this.stations = stations
  4.     this.index = 0
  5.  
  6.     // Setup the display for each station.
  7.     for (let i = 0; i < this.stations.length; i++) {
  8.       window[`title${i}`].innerHTML = this.title(this.stations[i].freq, this.stations[i].title)
  9.       window[`station${i}`].addEventListener('click', (index => {
  10.         const isNotPlaying = (this.stations[index].howl && !this.stations[index].howl.playing())
  11.  
  12.         radio.stop()
  13.  
  14.         if (isNotPlaying || !this.stations[index].howl) {
  15.           radio.play(index)
  16.         }
  17.       }).bind(this, i))
  18.     }
  19.   }
  20.  
  21.   title (freq, title) {
  22.     return `<b>${freq}</b> ${title}`
  23.   }
  24.  
  25.   play (index) {
  26.     let sound
  27.  
  28.     index = typeof index === 'number' ? index : this.index
  29.     const data = this.stations[index]
  30.  
  31.     if (data.howl) {
  32.       sound = data.howl
  33.     } else {
  34.       sound = data.howl = new Howl({
  35.         src: data.src,
  36.         html5: true,
  37.         format: ['mp3', 'aac']
  38.       })
  39.     }
  40.  
  41.     sound.play()
  42.     this.toggleStationDisplay(index, true)
  43.     this.index = index
  44.   }
  45.  
  46.   stop () {
  47.     const sound = this.stations[this.index].howl
  48.     this.toggleStationDisplay(this.index, false)
  49.     if (sound) sound.stop()
  50.   }
  51.  
  52.   toggleStationDisplay (index, state) {
  53.     window[`station${index}`].style.backgroundColor = state ? 'rgba(255, 255, 255, 0.33)' : ''
  54.     window[`live${index}`].style.opacity = state ? 1 : 0
  55.     window[`playing${index}`].style.display = state ? 'block' : 'none'
  56.   }
  57. }
  58.  
  59. const radio = new Radio([{
  60.   freq: '91.1',
  61.   title: 'Radio City',
  62.   src: 'http://prclive1.listenon.in:9960/',
  63.   howl: null
  64. }, {
  65.   freq: '81.4',
  66.   title: 'BBC Radio 1',
  67.   src: 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_q',
  68.   howl: null
  69. }, {
  70.   freq: '89.9',
  71.   title: 'Hip Hop Hits',
  72.   src: 'http://tunein4.streamguys1.com/hhbeafree5',
  73.   howl: null
  74. }, {
  75.   freq: '98.3',
  76.   title: 'Radio  Mirchi',
  77.   src: 'http://peridot.streamguys.com:7150/Mirchi',
  78.   howl: null
  79. }, {
  80.   freq: '103.3',
  81.   title: '80\'s Hits',
  82.   src: 'http://tunein4.streamguys1.com/80shtfree1',
  83.   howl: null
  84. }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement