0.6.24 • Published 2 years ago

radio4000-player v0.6.24

Weekly downloads
106
License
GPL-3.0
Repository
-
Last release
2 years ago

Radio4000 Player

This is a web component that plays channels from Radio4000. It uses the public radio4000-api and Vue.js.

npm.io

How to use

You can load the player with a CDN, like this:

<script async src="https://cdn.jsdelivr.net/npm/radio4000-player"></script>
<radio4000-player channel-slug="good-time-radio"></radio4000-player>

OR, you can use a package manager:

// npm install radio4000-player
import 'radio4000-player'
// now you can use <radio4000-player> in your templates

OR, you can use an iframe (source):

<iframe src="https://api.radio4000.com/embed?slug=detecteve" width="320" height="500" frameborder="0"></iframe>

API

Attributes

Here's a complete list of all the attributes you can set and change on the web component. These do not affect the iframe version.

AttributeTypeDescription
channel-slugstringRadio4000 channel slug (ex: oskar)
channel-idstringRadio4000 channel id (ex: -JYZvhj3vlGCjKXZ2cXO)
track-idstringRadio4000 track id (ex: -JYEosmvT82Ju0vcSHVP)
volumeintegerfrom 0 to 100 (default: 100)
autoplayboolean falseif it should start playing automatically, if volume is set to 0. See note.
shuffleboolean falseif tracks should be shuffled
r4-urlboolean falseuse relative, internal links. When the player is used on radio4000.com we want the links to not open a new window
showHeaderboolean falseDisplay or not the header part of the player. Where the channel image and current track are displayed
showTracklistboolean falseDisplay or not the player's list of tracks
showControlsboolean falseDisplay or not the player's controls. Where the play, mute and next buttons are
hostRootUrlstringRoot URL used to build links to the channel and tracks query, defaults to 'radio4000.com'
platformboolean falseIs this player used on a platform? If true, build URL starting from /, and use target="_self" to not reload the page

Note: If you change showTracklist showControls showHeader, to true, you will have the effect of a fullscreen mode for the player. In which only the media is displayed. Mostly usefull for a tv effect, to easily display a channel on a website, for example as a animated background. See autoplay.

Note: To have autoplay working, you should also set the volume property to 0. It has for effect of muting the autio of all media played in the player, and this is required for the autoplay feature to work. This requirement is enforced by browsers (Firefox, Chromium, etc.), so the User Experience forbits video to be autoplayed on websites, if there is also audio.

Examples

The are three available attributes to start the player: channel-slug, channel-id and track-id.

In a HTML file, add the following to generate three players:

<script async src="https://cdn.jsdelivr.net/npm/radio4000-player"></script>

<radio4000-player channel-slug="good-time-radio"></radio4000-player>
<radio4000-player channel-id="-JYZvhj3vlGCjKXZ2cXO"></radio4000-player>
<radio4000-player track-id="-JYEosmvT82Ju0vcSHVP"></radio4000-player>

You can also set and update the player attributes using JavaScript. Remember that HTML attributes are dasherized channel-slug whereas JavaScript expects CamelCase channelSlug.

var player = document.querySelector('radio4000-player')

// Load radio channel either by slug or id.
player.channelSlug = 'sugar-hiccup'
player.channelId = '-JYZvhj3vlGCjKXZ2cXO'

// Load a specific track.
player.trackId = '-JYEosmvT82Ju0vcSHVP'

// Change the volume.
player.volume = 25

// Enable shuffle.
player.shuffle = true

To enable autoplay:

<radio4000-player channel-slug="200ok" autoplay="true"></radio4000-player>

Events

You can listen for events directly on each <radio4000-player> element.

Event nameDescriptionArguments
playerReadyFires when ready
trackChangedFires whenever the current track is changed{previousTrack, track}
trackEndedFires when the current track finishes playing{track}
mediaNotAvailableFires when a track can not be played by its provider (YouTube etc.){track}

Here's an example of how to listen for the trackChanged event. It is the same approach for all events. In the case of both trackChanged and trackEnded, the event.detail[0] argument will be a Radio4000 track object.

var player = document.querySelector('radio4000-player')
player.addEventListener('trackChanged', (event) => {
  console.info('trackChanged event', event.detail[0])
  // { track: { [...] } }
})

Methods

!!! Danger zone. We are still finalizing the API for methods so expect this to change.

It is possible to load data to the <radio4000-player> without any relation to the Radio4000 database. To do that use the updatePlaylist method, on the JavaScript instance of the player. It accepts a playlist object.

The playlist object

AttributeTypeDescription
titlestringDisplayed in the header
imagestringURL to an image (square ~60px)
tracksstringAn array of track objects

Here's an example an array of a playlist object. Note how the tracks' url key points to remote media in the ogg format (can be any format your browser supports).

// Create a playlist.
const playlist = {
  title: 'A title for this list',
  image: 'https://78.media.tumblr.com/5080191d7d19fe64da558f2b4324563e/tumblr_p8eoiltn1t1twkjb3o1_1280.png',
  tracks: [
    {
      id: '1',
      title: 'Randomfunk.ogg',
      url: 'https://ia801409.us.archive.org/5/items/DWK051/Rare_and_Cheese_-_01_-_Randomfunk.ogg'
    },
    {
      id: '2',
      title: 'Rare and Cheese - Jazzpolice',
      url: 'https://ia801409.us.archive.org/5/items/DWK051/Rare_and_Cheese_-_02_-_Jazzpolice.ogg'
    }
  ]
}

// Get access to the Vue component behind the web component to access methods,
// and update player with our new playlist.
var player = document.querySelector('radio4000-player')
var vue = player.getVueInstance()
vue.updatePlaylist(playlist)

The track object

The track object attributes are based on the track model of Radio4000 API. Available attributes can be found there: github.com/internet4000/radio4000-api#track

This is an array of track, which have a property ytid, so the <radio4000-player> will read them with YouTube's iframe player.

const tracks = {
    {
        "id": "-JYZtlEKiZY75Wt6QpA5",
        "title": "Kleeer - Tonight",
        "url":" https://www.youtube.com/watch?v=cVXURwACwtk",
        "ytid": "cVXURwACwtk"
    },
    {
        "id": "-Jf2HYHsVl7iPDGZNbCa",
        "title":" Patrick Watson - Adventures In Your Own Backyard (Altered Route Video Edit)",
        "url":" https://www.youtube.com/watch?v=cbSbbY5ibas",
        "ytid": "cbSbbY5ibas"
    }
}

If instead of a ytid (being a Youtube video id), you use the key url, the player will attempt to read the media to which this url points to. It will use an HTML <audio> element, to which the supported media type list can be found on Mozilla MDN documentation.

Development

  • Feature branches are made from the master branch.
# 1. clone and install dependencies
git clone git@github.com:internet4000/radio4000-player.git
npm install

# 2. starts a server on http://localhost:4002 that autobuilds when files change
npm start

Testing

# run tests once
npm test

# (re)run tests as files change
npm start; npm run cypress open

How to release a new version

Do not use npm publish directly! (unless you can remember to npm run build before releasing)

Release a new patch e.g. 1.0.4 to 1.0.5.

npm run release

Release a minor, major, or specific version:

npm run release minor
npm run release major
npm run release 0.8.3

Running this command create a new, tagged "release" commit including a production build and pushes it to Git and NPM. See release-it for more.

Demo

The ./demo/index.html file will be deployed to player.radio4000.com via Netlify.

0.6.20

2 years ago

0.6.24

2 years ago

0.6.18

2 years ago

0.6.17

2 years ago

0.6.19

2 years ago

0.6.16

3 years ago

0.6.15

3 years ago

0.6.12

3 years ago

0.6.14

3 years ago

0.6.13

3 years ago

0.6.11

4 years ago

0.6.10

5 years ago

0.6.9

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago