Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Radio {
- constructor (stations) {
- this.stations = stations
- this.index = 0
- // Setup the display for each station.
- for (let i = 0; i < this.stations.length; i++) {
- window[`title${i}`].innerHTML = this.title(this.stations[i].freq, this.stations[i].title)
- window[`station${i}`].addEventListener('click', (index => {
- const isNotPlaying = (this.stations[index].howl && !this.stations[index].howl.playing())
- radio.stop()
- if (isNotPlaying || !this.stations[index].howl) {
- radio.play(index)
- }
- }).bind(this, i))
- }
- }
- title (freq, title) {
- return `<b>${freq}</b> ${title}`
- }
- play (index) {
- let sound
- index = typeof index === 'number' ? index : this.index
- const data = this.stations[index]
- if (data.howl) {
- sound = data.howl
- } else {
- sound = data.howl = new Howl({
- src: data.src,
- html5: true,
- format: ['mp3', 'aac']
- })
- }
- sound.play()
- this.toggleStationDisplay(index, true)
- this.index = index
- }
- stop () {
- const sound = this.stations[this.index].howl
- this.toggleStationDisplay(this.index, false)
- if (sound) sound.stop()
- }
- toggleStationDisplay (index, state) {
- window[`station${index}`].style.backgroundColor = state ? 'rgba(255, 255, 255, 0.33)' : ''
- window[`live${index}`].style.opacity = state ? 1 : 0
- window[`playing${index}`].style.display = state ? 'block' : 'none'
- }
- }
- const radio = new Radio([{
- freq: '91.1',
- title: 'Radio City',
- src: 'http://prclive1.listenon.in:9960/',
- howl: null
- }, {
- freq: '81.4',
- title: 'BBC Radio 1',
- src: 'http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_q',
- howl: null
- }, {
- freq: '89.9',
- title: 'Hip Hop Hits',
- src: 'http://tunein4.streamguys1.com/hhbeafree5',
- howl: null
- }, {
- freq: '98.3',
- title: 'Radio Mirchi',
- src: 'http://peridot.streamguys.com:7150/Mirchi',
- howl: null
- }, {
- freq: '103.3',
- title: '80\'s Hits',
- src: 'http://tunein4.streamguys1.com/80shtfree1',
- howl: null
- }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement