Skip to main content

Build a music service

BetaThe plugin SDK and these pages are new. They change with each release, as developers use them and tell us what works.View as Markdown

Create a plugin that adds a music service to Volumio: an entry in Browse, tracks that play, and results in search. The SDK's music_service template does all three with two placeholder tracks. You replace the placeholders with calls to your service.

Prerequisites​

  • Node.js 22 or later, and a Volumio 4 device on your network.
  • Your service's function from the function registry. If no function fits, use a new id in lowercase words joined by hyphens, and name it in the Plugins forum.
  • A way to get a stream URL for each track from your service, such as an HTTP API.

1. Create the plugin​

Run init with --type music_service:

npx --package=@volumio/plugin-sdk volumio-plugin init my_service \
--id my_service --type music_service --function <function> \
--title "My Service" --description "Plays My Service on Volumio"

Besides the files every plugin has, the template adds:

WhatWhere
Two placeholder trackscatalog.json
The Browse entry, play and searchindex.js: browseSource, handleBrowseUri, explodeUri, search
A setting for the name shown in BrowseUIConfig.json, config.json
The browse-source and playback permissionsvolumio-plugin.json

2. Keep every URI under the plugin's root​

Every URI the plugin hands to Volumio starts with music_service/<id>. The template keeps it in one constant:

index.js
const ROOT = 'music_service/my_service';
const trackUri = (track) => `${ROOT}/track/${track.id}`;

Volumio sends a Browse request to the first source whose URI starts the requested one, after its own sources. A root that is only the plugin's id can lose its pages to Volumio's favourites, playlists, radio and UPnP sources.

3. Fill Browse​

handleBrowseUri(uri) returns the lists to show for a URI. Replace the catalog with a call to your service. The call can be async: turn its result into a kew promise where Volumio calls in.

index.js
handleBrowseUri(uri) {
if (uri !== ROOT) return libQ.reject(new Error(`my_service: no page for ${uri}`));
const defer = libQ.defer();
this.fetchTracks().then(
(tracks) => defer.resolve({
navigation: {
prev: { uri: '/' },
lists: [{ availableListViews: ['list', 'grid'], items: tracks.map((track) => this.listItem(track)) }],
},
}),
(error) => defer.reject(error),
);
return defer.promise;
}

An item of type folder opens another page: Volumio calls handleBrowseUri again with the item's uri. An item of type song plays. Music service API lists every field.

4. Play a track​

explodeUri(uri) returns what a URI plays, as a list of tracks. With service: 'mpd' and the stream URL as uri, Volumio hands the stream to MPD. MPD then handles pause, stop and seek, so the plugin needs no playback code.

index.js
explodeUri(uri) {
const defer = libQ.defer();
this.fetchTrack(uri).then(
(track) => defer.resolve([{
service: 'mpd',
type: 'track',
uri: track.streamUrl,
name: track.title,
title: track.title,
artist: track.artist,
album: track.album,
albumart: track.coverUrl,
}]),
(error) => defer.reject(error),
);
return defer.promise;
}

Return a list even for one track. The queue adds every track in it, and Favourites reads the first.

5. Answer searches​

search({ value }) resolves null when nothing matches, or one list of items. Volumio searches every source at once. One rejected search can lose the results of all the others, so search never rejects and never throws:

index.js
search(query) {
const defer = libQ.defer();
this.searchService(String(query?.value ?? ''), { timeoutMs: 4000 }).then(
(tracks) => defer.resolve(tracks.length
? { title: this.strings.PLUGIN_NAME, availableListViews: ['list', 'grid'], items: tracks.map((t) => this.listItem(t)) }
: null),
(error) => {
this.logger.error(`my_service: search failed: ${error.message}`);
defer.resolve(null);
},
);
return defer.promise;
}

When Volumio searches every source, it stops waiting for a plugin after 5 seconds. From the plugin's own Browse page, it waits as long as the plugin takes. Put a time limit on every request search makes.

6. Declare the hosts​

In volumio-plugin.json, list every host the plugin connects to in network, stream hosts included:

volumio-plugin.json
"network": ["api.example.com", "*.stream.example.com"]

Volumio does not block other hosts. The list tells users and moderators what the plugin talks to.

7. Check that it works​

  1. Run npm test and npx --package=@volumio/plugin-sdk volumio-plugin check .. Both must pass.
  2. Install the plugin with npx --package=@volumio/plugin-sdk volumio-plugin deploy 192.168.1.20 ..
  3. In the Volumio web UI, open Browse. Your service is there, under the name set in its settings page.
  4. Open it and play a track. Search for a track title and find it among the results.

A service that plays by itself​

Some services give no stream URL: the plugin drives its own player. Then the track's service is the plugin's id, and the plugin implements clearAddPlayTrack, stop, pause, resume and seek. It reports every change of state with servicePushState. Music service API describes these methods.

Was this page helpful?