Build a music service
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:
| What | Where |
|---|---|
| Two placeholder tracks | catalog.json |
| The Browse entry, play and search | index.js: browseSource, handleBrowseUri, explodeUri, search |
| A setting for the name shown in Browse | UIConfig.json, config.json |
The browse-source and playback permissions | volumio-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:
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.
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.
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:
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:
"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
- Run
npm testandnpx --package=@volumio/plugin-sdk volumio-plugin check .. Both must pass. - Install the plugin with
npx --package=@volumio/plugin-sdk volumio-plugin deploy 192.168.1.20 .. - In the Volumio web UI, open Browse. Your service is there, under the name set in its settings page.
- 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.
Related
- Music service API
- Add a settings page, for an account or a token the user types
- Publish your plugin