I'm trying to store the current time of an embedded YouTube video in a shiny app upon a button click. Eventually I'd like to be able to bring that time data back into my R environment, but for now I'm just struggling with how to use the YouTube API within shiny.
I know from looking at the YouTube API and from this question that you can get the current time of an embedded YouTube video. But when I wrap that video in the Shiny interface, the video has class shiny-html-output shiny-bound-output and I can't seem to find the underyling element that corresponds to the YouTube video that's controllable by the api.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs()
,titlePanel("Hello Shiny!")
,sidebarLayout(
sidebarPanel(
actionButton("button", "Capture Video Time")
),
mainPanel(
uiOutput("video")
)
)
)
server <- function(input, output) {
output$video <- renderUI({
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/FR4QIeZaPeM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
})
observeEvent(input$button, {
runjs(
"ytplayer = document.getElementById('video');
var time = ytplayer.getCurrentTime();
alert(time);"
)
})
}
shinyApp(ui = ui, server = server)
What I expect is that when I click the button in the Shiny app, a window should pop up with the current time of the YouTube video, but instead nothing happens. So what my biggest need is to get that javascript code right inside the runjs()
function (or something else if runjs()
isn't appropriate here) so that I can actually find the video player's time. If you have any insight on how to bring that current time value back into my R environment it would be appreciated as well, but I can probably figure that part out once I get on the right track with this. Thanks so much in advance!