Skip to content

Commit e88aa8a

Browse files
authored
feat: plugins/web: use media session api for notifications. (#5714)
The Media Session API provides a way to customize media notifications. This PR has commits that, in summary, updates the metadata for the media session whenever a new track (item) starts playing. https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API
2 parents 4ddd782 + a75d2b4 commit e88aa8a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

beetsplug/web/static/beets.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ var AppView = Backbone.View.extend({
266266
playItem: function(item) {
267267
var url = 'item/' + item.get('id') + '/file';
268268
$('#player audio').attr('src', url);
269-
$('#player audio').get(0).play();
269+
$('#player audio').get(0).play().then(() => {
270+
this.updateMediaSession(item);
271+
});
270272

271273
if (this.playingItem != null) {
272274
this.playingItem.entryView.setPlaying(false);
@@ -275,6 +277,26 @@ var AppView = Backbone.View.extend({
275277
this.playingItem = item;
276278
},
277279

280+
updateMediaSession: function (item) {
281+
if ("mediaSession" in navigator) {
282+
const album_id = item.get("album_id");
283+
const album_art_url = "album/" + album_id + "/art";
284+
navigator.mediaSession.metadata = new MediaMetadata({
285+
title: item.get("title"),
286+
artist: item.get("artist"),
287+
album: item.get("album"),
288+
artwork: [
289+
{ src: album_art_url, sizes: "96x96" },
290+
{ src: album_art_url, sizes: "128x128" },
291+
{ src: album_art_url, sizes: "192x192" },
292+
{ src: album_art_url, sizes: "256x256" },
293+
{ src: album_art_url, sizes: "384x384" },
294+
{ src: album_art_url, sizes: "512x512" },
295+
],
296+
});
297+
}
298+
},
299+
278300
audioPause: function() {
279301
this.playingItem.entryView.setPlaying(false);
280302
},

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Unreleased
88

99
New features:
1010

11+
* :doc:`plugins/web`: Show notifications when a track plays. This uses the
12+
Media Session API to customize media notifications.
13+
1114
Bug fixes:
1215

1316
For packagers:

0 commit comments

Comments
 (0)