Spotify Audio Web Component

1.0.4 · active · verified Tue Apr 21

The `spotify-audio-element` package provides a custom web component, `<spotify-audio>`, designed to embed and control Spotify audio playback directly within HTML. It aims to replicate the standard HTML `<audio>` element's API, offering a familiar interface for developers. Currently at version `1.0.4`, this package is part of the `muxinc/media-elements` monorepo, which sees active development across its various media component offerings. While `spotify-audio-element` itself might not have frequent individual version bumps, the monorepo has consistent updates across its related packages. Its primary differentiators include a compliant `HTMLMediaElement` API surface, making it intuitive for those familiar with native audio elements, and seamless integration capabilities with `Media Chrome` for advanced UI customization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and instantiate the `spotify-audio` custom element, set its source, append it to the DOM, and programmatically control playback. It uses an environment variable for a Spotify Track URI to ensure a runnable example.

import 'spotify-audio-element';

const spotifyPlayer = document.createElement('spotify-audio');
spotifyPlayer.setAttribute('controls', '');
// Use a Spotify track URI for direct playback, e.g., 'spotify:track:11dFghVXANMlKmJXsNCbNl' for a public domain track.
// A Spotify artist URI as in the README example ('spotify:artist:246dkjvS1zLTtiykXe5h60') generally only loads the artist page, not playable audio.
// Replace with a valid Spotify track URI or use an environment variable.
spotifyPlayer.setAttribute('src', process.env.SPOTIFY_TRACK_URI ?? 'spotify:track:11dFghVXANMlKmJXsNCbNl');

document.body.appendChild(spotifyPlayer);

// Example of programmatic control after element is added to DOM
spotifyPlayer.addEventListener('loadedmetadata', () => {
  console.log('Spotify player loaded metadata.');
  spotifyPlayer.play();
});

spotifyPlayer.addEventListener('play', () => {
  console.log('Playback started!');
});

spotifyPlayer.addEventListener('error', (e) => {
  console.error('Spotify player error:', e);
});

view raw JSON →