Build a music provider
A music provider adds a source of music: a streaming service, a radio directory, a media server or your own files. This page covers what the server asks of its code.
Start from _demo_music_provider: it has every method, with a comment that says when the server calls it and what it returns. A provider that talks to an online service also follows the rules for providers that stream, and caches and throttles its requests.
The class
Section titled “The class”Your provider subclasses MusicProvider. __init__.py has a module-level setup() that returns the instance:
SUPPORTED_FEATURES = { ProviderFeature.SEARCH, ProviderFeature.LIBRARY_TRACKS, ProviderFeature.ARTIST_ALBUMS,}
async def setup( mass: MusicAssistant, manifest: ProviderManifest, config: ProviderConfig) -> ProviderInstanceType: """Initialize provider(instance) with given configuration.""" return MyMusicProvider(mass, manifest, config, SUPPORTED_FEATURES)setup()creates the instance and passes it the features. Preflight checks are allowed; it must return the instance.handle_async_init()runs after the config is loaded: connect and sign in there. RaiseLoginFailedfor wrong credentials, so the user sees it and the server does not retry.get_config_entries()returns the options. A login or other one-time input comes from a setup flow: see Setup flows.unload(is_removed)closes connections and stops tasks.
Features
Section titled “Features”The set of ProviderFeature values you pass to the constructor says what your provider can do. The server only calls a method when its feature is in the set: search() for SEARCH, get_library_tracks() and get_track() for LIBRARY_TRACKS, get_artist_albums() for ARTIST_ALBUMS. The demo provider names the feature at every method. Do not override the supported_features property.
provides in the manifest lists the media types a user looks for your provider under, whatever its config, such as ["podcast", "audiobook"] for an audiobook server or ["radio"] for a radio directory. The Marketplace filters on it before the provider is loaded; what the provider does at runtime always comes from its code. A test fails when a built-in provider lists a media type it has no method for.
Library sync
Section titled “Library sync”The server syncs the user’s library with each provider on a schedule, which the provider can set in get_default_library_sync_schedule. See Library, sync and metadata for what happens to the items.
sync_library(media_type)iterates the generator of that type:get_library_artists(),get_library_albums(),get_library_tracks(),get_library_playlists(),get_library_radios(),get_library_audiobooks()orget_library_podcasts().- Yield every item in the user’s library on every run. The server works out what was added and what was removed.
- When a generator skips an item because of an error, call
report_skipped_sync_item(), so the item is not taken for removed. - Podcasts are synced, their episodes are not: the server asks
get_podcast_episodes()when it needs them. - Override
sync_library()only when you need control over the sync itself.
Playback
Section titled “Playback”Playback has two steps:
get_stream_details(item_id, media_type)returnsStreamDetails: thestream_type, theaudio_formatas far as you know it, and for most types thepath(a URL or a local file). The server also calls it to load the next item ahead of time, so it has no side effects: report playback to the service inon_stream_started()andon_streamed().- Only for
StreamType.CUSTOM, the server callsget_audio_stream(streamdetails, seek_position), an async generator that yields the audio bytes. For every other type the server openspathitself.
Three providers to learn from:
- Spotify: a custom stream, read from an external program (librespot, or Spotify’s own soloist client).
- YouTube Music: an HTTP URL that expires. It sets
expirationfrom the URL, so the server asks for fresh details before the URL lapses. - Qobuz: an HTTP URL with the exact format from the service, and playback reported back in
on_stream_started().
Podcast episodes
Section titled “Podcast episodes”Set the position of each episode so that the oldest has the lowest number and the newest the highest. The episode list is sorted on it, and “play from here” queues the selected episode and everything newer. Rank on the publication date with rank_episodes_by_date() in podcast_parsers.py, not on the order your API returns them, and leave position at 0 where it is unknown.
Stored image paths
Section titled “Stored image paths”An image path that is not a URL, such as a cover id that your resolve_image() fetches from the service, is dropped when it is read back from a playlist file, because a stored path could point at a local file. When your resolve_image() only looks such a path up in your own service, override accepts_stored_image_path() to accept the paths your service uses; resolve_image() must then return image data or a remote URL for them. Plex is an example. Accepting every non-empty path is only safe when resolve_image() sends it to your service and uses it for nothing else, as OpenSubsonic does with its cover art ids.
Music Assistant is a project of the Open Home Foundation.