aboutsummaryrefslogtreecommitdiff
path: root/src/frontend
diff options
context:
space:
mode:
authorMiguel Ángel Moreno <mail@migalmoreno.com>2024-01-22 01:13:50 +0100
committerMiguel Ángel Moreno <mail@migalmoreno.com>2024-01-22 01:13:50 +0100
commit284ff27c86e834428ec57d26fb5ee583c8d263db (patch)
treeab59f85b78fb84a50c676a4bdae095de74c5a262 /src/frontend
parentc29e709decfee4d7535ed3affb271472cc7ed053 (diff)
feat(frontend): add MediaSession API effects
Diffstat (limited to 'src/frontend')
-rw-r--r--src/frontend/tubo/events.cljs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/frontend/tubo/events.cljs b/src/frontend/tubo/events.cljs
index 2c21dae..309aa93 100644
--- a/src/frontend/tubo/events.cljs
+++ b/src/frontend/tubo/events.cljs
@@ -2,6 +2,7 @@
(:require
[akiroz.re-frame.storage :refer [reg-co-fx!]]
[day8.re-frame.http-fx]
+ [goog.object :as gobj]
[re-frame.core :as rf]
[reitit.frontend.easy :as rfe]
[reitit.frontend.controllers :as rfc]
@@ -71,6 +72,46 @@
(if paused?
(.play player)
(.pause player)))))
+(rf/reg-fx
+ ::stream-metadata
+ (fn [metadata]
+ (when (gobj/containsKey js/navigator "mediaSession")
+ (set! (.-metadata js/navigator.mediaSession) (js/MediaMetadata. (clj->js metadata))))))
+
+(rf/reg-fx
+ ::media-session
+ (fn [{:keys [current-pos player stream]}]
+ (when (gobj/containsKey js/navigator "mediaSession")
+ (let [updatePositionState
+ #(.setPositionState js/navigator.mediaSession
+ {:duration (.-duration @player)
+ :playbackRate (.-playbackRate @player)
+ :position (.-currentTime @player)})]
+ (.setActionHandler js/navigator.mediaSession "play" #(.play @player))
+ (.setActionHandler js/navigator.mediaSession "pause" #(.pause @player))
+ (.setActionHandler js/navigator.mediaSession "previoustrack"
+ #(rf/dispatch [::change-media-queue-pos (- current-pos 1)]))
+ (.setActionHandler js/navigator.mediaSession "nexttrack"
+ #(rf/dispatch [::change-media-queue-pos (+ current-pos 1)]))
+ (.setActionHandler js/navigator.mediaSession "seekbackward"
+ (fn [^js/navigator.MediaSessionActionDetails details]
+ (set! (.-currentTime @player)
+ (- (.-currentTime @player) (or (.-seekOffset details) 10)))
+ (updatePositionState)))
+ (.setActionHandler js/navigator.mediaSession "seekforward"
+ (fn [^js/navigator.MediaSessionActionDetails details]
+ (set! (.-currentTime @player)
+ (+ (.-currentTime @player) (or (.-seekOffset details) 10)))
+ (updatePositionState)))
+ (.setActionHandler js/navigator.mediaSession "seekto"
+ (fn [^js/navigator.MediaSessionActionDetails details]
+ (set! (.-currentTime @player) (.-seekTime details))
+ (updatePositionState)))
+ (.setActionHandler js/navigator.mediaSession "stop"
+ (fn []
+ (.pause @player)
+ (set! (.-currentTime @player) 0)))))))
+
(rf/reg-event-fx
::history-go
(fn [_ [_ idx]]