feat: add media session API on now-playing update

This commit is contained in:
Luis Pereira 2026-02-20 17:26:14 +00:00 committed by Brian O'Reilly
parent 61266647a9
commit 8700724f81
5 changed files with 832 additions and 794 deletions

View File

@ -49,7 +49,8 @@
(:file "template-utils") (:file "template-utils")
(:file "parenscript-utils") (:file "parenscript-utils")
(:module :parenscript (:module :parenscript
:components ((:file "recently-played") :components ((:file "parenscript-utils")
(:file "recently-played")
(:file "auth-ui") (:file "auth-ui")
(:file "front-page") (:file "front-page")
(:file "profile") (:file "profile")

View File

@ -250,6 +250,13 @@
;; Check if this track is in user's favorites ;; Check if this track is in user's favorites
(check-favorite-status) (check-favorite-status)
;; Update favorite count display ;; Update favorite count display
(update-favorite-information)
(update-media-session new-title)))))))))
(catch (lambda (error)
(ps:chain console (log "Could not fetch stream status:" error)))))))
;; Update favorite count display
(defun update-favorite-information ()
(let ((count-el (ps:chain document (get-element-by-id "favorite-count-display"))) (let ((count-el (ps:chain document (get-element-by-id "favorite-count-display")))
(count-val-el (ps:chain document (get-element-by-id "favorite-count-value")))) (count-val-el (ps:chain document (get-element-by-id "favorite-count-value"))))
(when (and count-el count-val-el) (when (and count-el count-val-el)
@ -259,9 +266,8 @@
(if (= fav-count 1) (if (= fav-count 1)
"1 person loves this track ❤️" "1 person loves this track ❤️"
(+ fav-count " people love this track ❤️"))) (+ fav-count " people love this track ❤️")))
(setf (ps:@ count-el text-content) ""))))))))))))) (setf (ps:@ count-el text-content) ""))))))
(catch (lambda (error)
(ps:chain console (log "Could not fetch stream status:" error)))))))
;; Update stream information ;; Update stream information
(defun update-stream-information () (defun update-stream-information ()
@ -635,6 +641,7 @@
;; Load user's favorites for highlight feature ;; Load user's favorites for highlight feature
(load-favorites-cache) (load-favorites-cache)
(update-favorite-information)
;; Update now playing ;; Update now playing
(update-now-playing) (update-now-playing)
@ -864,4 +871,6 @@
(defun generate-front-page-js () (defun generate-front-page-js ()
"Return the pre-compiled JavaScript for front page" "Return the pre-compiled JavaScript for front page"
*front-page-js*) (ps-join
*common-player-js*
*front-page-js*))

View File

@ -3,9 +3,30 @@
(in-package #:asteroid) (in-package #:asteroid)
(defparameter *player-js* (defparameter *common-player-js*
(ps:ps* (ps:ps*
'(progn '(progn
(defun update-media-session (title)
(let ((media-session (ps:@ navigator media-session)))
(when media-session
(let ((track-title "Unknown")
(now-playing-title-el (ps:chain document (query-selector "#current-track-title"))))
(when title
(setf track-title title))
(when (and now-playing-title-el (not title))
(let ((now-playing-title (ps:@ now-playing-title-el text-content)))
(when now-playing-title
(setf track-title now-playing-title))))
(let* ((media-info (ps:create :title track-title
:artwork (list (ps:create :src "/asteroid/static/asteroid-squared.png"
:type "image/png"
:sizes "256x256"))))
(metadata (ps:new (-media-metadata media-info))))
(setf (ps:@ media-session metadata) metadata)))))))))
(defparameter *player-js*
(ps:ps*
`(progn
;; Global variables ;; Global variables
(defvar *tracks* (array)) (defvar *tracks* (array))
@ -781,7 +802,8 @@
(ps:chain console (log "Error connecting to stream")) (ps:chain console (log "Error connecting to stream"))
""))))) "")))))
(then (lambda (data) (then (lambda (data)
(setf (ps:chain document (get-element-by-id "now-playing") inner-h-t-m-l) data))) (setf (ps:chain document (get-element-by-id "now-playing") inner-h-t-m-l) data)
(update-media-session)))
(catch (lambda (error) (catch (lambda (error)
(ps:chain console (log "Could not fetch stream status:" error)))))) (ps:chain console (log "Could not fetch stream status:" error))))))
@ -810,4 +832,6 @@
(defun generate-player-js () (defun generate-player-js ()
"Generate JavaScript code for the web player" "Generate JavaScript code for the web player"
*player-js*) (ps-join
*common-player-js*
*player-js*))

View File

@ -524,6 +524,7 @@
(setf (ps:@ el text-content) title) (setf (ps:@ el text-content) title)
;; Check if this track is in user's favorites ;; Check if this track is in user's favorites
(check-favorite-status-mini)) (check-favorite-status-mini))
(update-media-session title)
(when track-id-el (when track-id-el
(let ((track-id (or (ps:@ data data track_id) (ps:@ data track_id)))) (let ((track-id (or (ps:@ data data track_id) (ps:@ data track_id))))
(setf (ps:@ track-id-el value) (or track-id "")))) (setf (ps:@ track-id-el value) (or track-id ""))))
@ -634,7 +635,8 @@
(when title-el (when title-el
(setf (ps:@ title-el text-content) (ps:chain track-text (trim)))) (setf (ps:@ title-el text-content) (ps:chain track-text (trim))))
(when artist-el (when artist-el
(setf (ps:@ artist-el text-content) "Asteroid Radio")))))))) (setf (ps:@ artist-el text-content) "Asteroid Radio")))))
(update-media-session track-text))))
(catch (lambda (error) (catch (lambda (error)
(ps:chain console (error "Error updating now playing:" error))))))) (ps:chain console (error "Error updating now playing:" error)))))))
@ -1082,4 +1084,6 @@
(defun generate-stream-player-js () (defun generate-stream-player-js ()
"Generate JavaScript code for the stream player" "Generate JavaScript code for the stream player"
*stream-player-js*) (ps-join
*common-player-js*
*stream-player-js*))

BIN
static/asteroid-squared.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB