Skip to content

Commit

Permalink
Add examples of playr's videoStats APIs
Browse files Browse the repository at this point in the history
Got a bunch of video stats that gets spit out onhover of a button.
  • Loading branch information
rtshkmr committed Jan 7, 2024
1 parent 9315974 commit 91e7c75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 15 additions & 2 deletions assets/js/hooks/youtube_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const TriggerYouTubeFunction = {
}
const {functionName, eventName} = this.el.dataset
const callback = callbacks[functionName]
const getOptions = () => ({...this.el.dataset, player: window.youtubePlayer})
const getOptions = () => ({hook: this,...this.el.dataset, player: window.youtubePlayer})
this.el.addEventListener(eventName, () => callback(getOptions()))
}
}
Expand All @@ -97,6 +97,19 @@ const callbacks = {
} = options;
console.log(`Loading video with id ${videoId} at t=${startSeconds}s`)
player.loadVideoById({videoId, startSeconds})
},
getAllStats: function(options) { // this is a custom function
const {
hook,
player,
} = options;
const stats = {
duration: player.getDuration(),
videoUrl: player.getVideoUrl(),
currentTime: player.getCurrentTime(),
}
console.log(">>> Retrieved stats:", stats)
hook.pushEventTo("#statsHover", "reportVideoStatus", stats)
}
}

Expand All @@ -110,7 +123,7 @@ const isYouTubeFnCallable = (dataset) => {
console.warn("Need to provide both valid function name and event name");
return false
}
const supportedEvents = ["click"]
const supportedEvents = ["click", "mouseover"]
if (!supportedEvents.includes(eventName)) {
console.warn(`${eventName} is not a supported event. Supported events include ${supportedEvents}.`);
return false
Expand Down
9 changes: 9 additions & 0 deletions lib/vyasa_web/components/youtube_player.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule VyasaWeb.YouTubePlayer do
use VyasaWeb, :live_component

@impl true
def render(assigns) do
~H"""
<div>
Expand All @@ -18,6 +19,7 @@ defmodule VyasaWeb.YouTubePlayer do
Load New Video @ Start
</.button>
<br/>
<.button id="statsHover" phx-hook={"TriggerYouTubeFunction"} data-event-name={"mouseover"} data-function-name={"getAllStats"}> Hover to get stats </.button>
<br/>
<div
crossorigin="anonymous"
Expand All @@ -27,4 +29,11 @@ defmodule VyasaWeb.YouTubePlayer do
</div>
"""
end

@impl true
def handle_event("reportVideoStatus", payload, socket) do
IO.inspect(payload)
{:noreply, socket}
end

end

0 comments on commit 91e7c75

Please sign in to comment.