1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-07 02:33:39 +02:00

Start restructuring audio playback, add some tests for audio

Fixes #15 where audio was not stopped on logout
This commit is contained in:
Arne Schlüter 2018-08-01 18:36:47 +02:00
commit 80225d46b1
10 changed files with 187 additions and 69 deletions

View file

@ -20,8 +20,10 @@
(doseq [event ["loadstart" "progress" "play" "timeupdate" "pause"]]
(.addEventListener el event #(re-frame/dispatch [:audio/update (->status el)]))))
;; effects to be fired from event handlers
(re-frame/reg-fx
:play-song
:audio/play
(fn [song-url]
(when-not @audio
(reset! audio (js/Audio.))
@ -31,9 +33,74 @@
(.play @audio)))
(re-frame/reg-fx
:toggle-play-pause
:audio/pause
(fn [_]
(let [a @audio]
(some-> @audio .pause)))
(re-frame/reg-fx
:audio/stop
(fn [_]
(when-let [audio @audio]
(.pause audio)
(set! (.-currentTime audio) 0))))
(re-frame/reg-fx
:audio/toggle-play-pause
(fn [_]
(if-let [a @audio]
(if (.-paused a)
(.play a)
(.pause a)))))
;; subscriptions
(defn summary
"Returns all information about audio that we have"
[db _]
(:audio db))
(re-frame/reg-sub :audio/summary summary)
(defn current-song
"Gives us information about the currently played song as presented by
the airsonic api"
[summary _]
(:current-song summary))
(re-frame/reg-sub
:audio/current-song
(fn [_ _] (re-frame/subscribe [:audio/summary]))
current-song)
(defn playback-status
"Gives us information about the most recently fired html 5 audio event"
[summary _]
(:playback-status summary))
(re-frame/reg-sub
:audio/playback-status
(fn [_ _] (re-frame/subscribe [:audio/summary]))
playback-status)
(defn is-playing?
"Predicate to tell us whether we currently have audio output or not"
[playback-status _]
(and (not (:paused? playback-status))
(not (:ended? playback-status))))
(re-frame/reg-sub
:audio/is-playing?
(fn [_ _] (re-frame/subscribe [:audio/current-playback-status]))
is-playing?)
(comment
;; NOTE: Not in use currently
(defn current-playlist
"Lists the complete playlist"
[summary _]
(:playlist summary))
(re-frame/reg-sub
:audio/current-playlist
(fn [_ _] (re-frame/subscribe [:audio/summary]))
current-playlist))