From 645c4bd1178f54de5f81e23be73d06275698e4a0 Mon Sep 17 00:00:00 2001 From: Michael Crumm Date: Thu, 3 Feb 2022 09:46:19 -0800 Subject: [PATCH] Add MediaMetadata to audio player when available --- assets/js/app.js | 6 +++++- lib/live_beats_web/live/player_live.ex | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/js/app.js b/assets/js/app.js index 476cdc1..bc622f0 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -116,7 +116,7 @@ Hooks.AudioPlayer = { this.play() } }) - this.handleEvent("play", ({url, token, elapsed}) => { + this.handleEvent("play", ({url, token, elapsed, artist, title}) => { this.playbackBeganAt = nowSeconds() - elapsed let currentSrc = this.player.src.split("?")[0] if(currentSrc === url && this.player.paused){ @@ -125,6 +125,10 @@ Hooks.AudioPlayer = { this.player.src = `${url}?token=${token}` this.play({sync: true}) } + + if("mediaSession" in navigator){ + navigator.mediaSession.metadata = new MediaMetadata({artist, title}) + } }) this.handleEvent("pause", () => this.pause()) this.handleEvent("stop", () => this.stop()) diff --git a/lib/live_beats_web/live/player_live.ex b/lib/live_beats_web/live/player_live.ex index 7e1ba91..aba490c 100644 --- a/lib/live_beats_web/live/player_live.ex +++ b/lib/live_beats_web/live/player_live.ex @@ -314,6 +314,8 @@ defmodule LiveBeatsWeb.PlayerLive do }) push_event(socket, "play", %{ + artist: song.artist, + title: song.title, paused: Song.paused?(song), elapsed: elapsed, duration: song.duration,