mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 02:33:39 +02:00
Add a real playlist (#20)
* Start implementing playlist Done so far: * Creation * Changing playback mode * Changing repeat mode * Add skipping for linear playlists with all repeat modes * Complete implementation for playlist/next-song * Implement all the playlist skipping functionality * Add functions to enqueue songs * Remove start-idx parameter when constructing playlists * Use namespaced keywords only when modifying external data E.g. songs in the queue
This commit is contained in:
parent
2638378064
commit
1888c3023c
11 changed files with 456 additions and 46 deletions
109
src/cljs/airsonic_ui/audio/core.cljs
Normal file
109
src/cljs/airsonic_ui/audio/core.cljs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
(ns airsonic-ui.audio.core
|
||||
"This namespace contains some JS interop code to interact with an audio player
|
||||
and receive information about the current playback status so we can use it in
|
||||
our re-frame app."
|
||||
(:require [re-frame.core :as re-frame]))
|
||||
|
||||
;; TODO: Manage buffering
|
||||
|
||||
(defonce audio (atom nil))
|
||||
|
||||
(defn ->status
|
||||
"Takes an audio object and returns a map describing its current status"
|
||||
[elem]
|
||||
{:ended? (.-ended elem)
|
||||
:loop? (.-loop elem)
|
||||
:muted? (.-muted elem)
|
||||
:paused? (.-paused elem)
|
||||
:current-src (.-currentSrc elem)
|
||||
:current-time (.-currentTime elem)})
|
||||
|
||||
; explanation of these events: https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery/Cross-browser_audio_basics
|
||||
(defn attach-listeners! [el]
|
||||
(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
|
||||
:audio/play
|
||||
(fn [song-url]
|
||||
(when-not @audio
|
||||
(reset! audio (js/Audio.))
|
||||
(attach-listeners! @audio))
|
||||
(.pause @audio)
|
||||
(set! (.-src @audio) song-url)
|
||||
(.play @audio)))
|
||||
|
||||
(re-frame/reg-fx
|
||||
:audio/pause
|
||||
(fn [_]
|
||||
(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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue