Liking cljdoc? Tell your friends :D

dk.simongray.play-nice.page.autoplay

The browser's autoplay policy, the wait for the click, tap or key press after which the page may play, and autoplay of a video while it's in view.

The policy and the wait take these opts:

  • :window, the window to ask and listen at instead of the page's, e.g. a stand-in in a test

Only Firefox has Autoplay Policy Detection, so elsewhere you find out by calling media/play!, which answers {:refused :not-allowed} where the browser won't play. play-when-allowed! plays then at the user's next click, tap or key press.

Chrome and WebKit on iOS pause a muted video with the autoplay attribute when it scrolls out of view, and play it again when it returns, but leave alone a video that the page plays itself. play-in-view! does the same for any video, in every engine, through media/play!:

(def stop!
  (play-in-view! hero #(swap! page assoc :hero %) {:threshold 0.5}))
The browser's autoplay policy, the wait for the click, tap or key press
after which the page may play, and autoplay of a video while it's in
view.

The policy and the wait take these opts:

- :window, the window to ask and listen at instead of the page's, e.g.
  a stand-in in a test

Only Firefox has Autoplay Policy Detection, so elsewhere you find out by
calling media/play!, which answers {:refused :not-allowed} where the
browser won't play. play-when-allowed! plays then at the user's next
click, tap or key press.

Chrome and WebKit on iOS pause a muted video with the autoplay attribute
when it scrolls out of view, and play it again when it returns, but
leave alone a video that the page plays itself. play-in-view! does the
same for any video, in every engine, through media/play!:

    (def stop!
      (play-in-view! hero #(swap! page assoc :hero %) {:threshold 0.5}))
raw docstring

activation-eventscljs

The events at which a page may gain user activation.

The events at which a page may gain user activation.
raw docstring

on-activation!cljs

(on-activation! f)
(on-activation! f opts)

Call f with no arguments once the page has user activation, with the opts above, and return a function that stops waiting.

It calls f at once where the page has activation now, e.g. in a click handler, and otherwise at the user's next click, tap or key press, e.g. to start the media that the browser refused to play:

(on-activation! #(media/play! audio))

Not every input activates the page, e.g. the Escape key doesn't, so the call asks the browser at each of activation-events. Where the browser can't tell, any of them counts.

Call `f` with no arguments once the page has user activation, with the
`opts` above, and return a function that stops waiting.

It calls `f` at once where the page has activation now, e.g. in a click
handler, and otherwise at the user's next click, tap or key press, e.g.
to start the media that the browser refused to play:

    (on-activation! #(media/play! audio))

Not every input activates the page, e.g. the Escape key doesn't, so the
call asks the browser at each of activation-events. Where the browser
can't tell, any of them counts.
raw docstring

play-in-view!cljs

(play-in-view! video f)
(play-in-view! video
               f
               {:keys [threshold reduced-motion] :or {threshold 0.5} :as opts})

Play the video while it's in view, and pause it while it isn't, with the opts below, and return a function that stops it. Call f with the state of the video now and whenever it changes, e.g.

{:visible true :playing true}

The video plays as media/play! with :muted-fallback plays it, and pauses out of view only while it's muted, as the engines do. Once the visitor pauses the video, the call leaves it to them, and the state holds :held as :visitor. Where the user prefers reduced motion, the call holds the video from the start, as :reduced-motion. A play that the browser refused shows as :refused, e.g. :not-allowed.

  • :threshold, the share of the video that must show for it to play, 0.5 by default
  • :reduced-motion, whether to hold the video, by default as the user's preference says, from reduced-motion?
Play the `video` while it's in view, and pause it while it isn't, with
the `opts` below, and return a function that stops it. Call `f` with the
state of the video now and whenever it changes, e.g.

    {:visible true :playing true}

The video plays as media/play! with :muted-fallback plays it, and pauses
out of view only while it's muted, as the engines do. Once the visitor
pauses the video, the call leaves it to them, and the state holds :held
as :visitor. Where the user prefers reduced motion, the call holds the
video from the start, as :reduced-motion. A play that the browser
refused shows as :refused, e.g. :not-allowed.

- :threshold, the share of the video that must show for it to play,
  0.5 by default
- :reduced-motion, whether to hold the video, by default as the user's
  preference says, from reduced-motion?
raw docstring

play-when-allowed!cljs

(play-when-allowed! element f)
(play-when-allowed! element
                    f
                    {:keys [muted-fallback] :or {muted-fallback true} :as opts})

Play the media element now, or where the browser refuses, once more at the user's next click, tap or key press, with the opts above and below. Call f with each answer of media/play!, and return a function that stops it, e.g.

(def stop!
  (play-when-allowed! audio #(swap! app assoc :answer %)))

The element plays muted where the browser refuses only its sound, which answers {:playing true :muted true}. Where it refuses even that, f gets {:refused :not-allowed :waiting true} while the call waits, and then the answer of the second try. There's no third, so where the browser never lets the page play, e.g. by Safari's Never Auto-Play setting, the wait ends with its refusal rather than loop.

  • :muted-fallback, false to wait for the user rather than play muted, true by default
Play the media `element` now, or where the browser refuses, once more at
the user's next click, tap or key press, with the `opts` above and below.
Call `f` with each answer of media/play!, and return a function that
stops it, e.g.

    (def stop!
      (play-when-allowed! audio #(swap! app assoc :answer %)))

The element plays muted where the browser refuses only its sound, which
answers {:playing true :muted true}. Where it refuses even that, `f`
gets {:refused :not-allowed :waiting true} while the call waits, and
then the answer of the second try. There's no third, so where the
browser never lets the page play, e.g. by Safari's Never Auto-Play
setting, the wait ends with its refusal rather than loop.

- :muted-fallback, false to wait for the user rather than play muted,
  true by default
raw docstring

policycljs

(policy target)
(policy target opts)

The browser's autoplay policy for target, with the opts above: :allowed, :allowed-muted or :disallowed, or nil where it can't tell.

The target is a media element, an AudioContext, or either's kind, :mediaelement or :audiocontext.

The browser's autoplay policy for `target`, with the `opts` above:
:allowed, :allowed-muted or :disallowed, or nil where it can't tell.

The `target` is a media element, an AudioContext, or either's kind,
:mediaelement or :audiocontext.
raw docstring

reduced-motion?cljs

(reduced-motion?)

Whether the user prefers reduced motion, as the media query prefers-reduced-motion says, or false where the browser can't tell.

Whether the user prefers reduced motion, as the media query
prefers-reduced-motion says, or false where the browser can't tell.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close