I am trying the code below using delay
from shinyjs
package.
What I am expecting is to show the modal AND play the audio after a predefined time from running the app (5 seconds in this example).
I only can see the modal but audio is not playing. any guidance on what might be wrong?
thank you
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
h2(textOutput("currentTime")),
br(),
br(),
h2(textOutput("target1")),
h2(textOutput("till1")),
uiOutput('my_audio1')
)
server <- function(input, output, session) {
output$currentTime <- renderText({
invalidateLater(1000, session)
paste("The current time is", format(Sys.time(), "%H:%M"))
})
t1 <- isolate({
Sys.time() + 5
})
output$target1 <- renderText({
paste("Target", t1)
})
output$till1 <- renderText({
invalidateLater(1000, session)
paste("Seconds till target:", round (t1-Sys.time()))
})
sys1 = isolate({
Sys.time()
})
observe({
print(sys1)
print(t1)
print(t1-sys1)
print(as.numeric(t1-sys1, units = "secs")*1000)
})
observe ({
delay (as.numeric(t1-sys1, units = "secs")*1000,
showModal(modalDialog(
title = "",
"Audio should play now"))
)
})
observe({
delay (as.numeric(t1-sys1, units = "secs")*1000,
output$my_audio1 <-renderUI({
tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3",
type = "audio/mp3", autoplay = NA, controls = NA, style="display:none;")
})
)
})
}
# Create Shiny app ----
shinyApp(ui, server)
Update: now, the audio files autoplay from the laptop browser but not from phone safari browser. Looks like this is an issue with almost all mobile browser (they disabled audio autoplay). I tried to implement this trick in the updated code below, but it is still not working R shiny not playing audio automatically
Any suggestions on where to go from there?
I read about a new package shinyMobile
, that can add progressive web app feature to shiny apps. do you think this might solve the problem and all delayed/scheduled audio to autoplay on mobile?
thank you
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
h2(textOutput("currentTime")),
tags$audio(src = "https://olafwempe.com/mp3/silence/silence.mp3",type = "audio/mp3", autoplay = TRUE),
br(),
br(),
h2(textOutput("target1")),
h2(textOutput("till1")),
actionButton("play", "Start"),
uiOutput('my_audio1')
)
server <- function(input, output, session) {
output$currentTime <- renderText({
invalidateLater(1000, session)
paste("The current time is", format(Sys.time(), "%H:%M"))
})
t1 <- isolate({
Sys.time() + 5
})
output$target1 <- renderText({
paste("Target", t1)
})
output$till1 <- renderText({
invalidateLater(1000, session)
paste("Seconds till target:", round (t1-Sys.time()))
})
sys1 = isolate({
Sys.time()
})
observeEvent(input$play, {
print(sys1)
print(t1)
print(t1-sys1)
print(as.numeric(t1-sys1, units = "secs")*1000)
})
observeEvent (input$play, {
delay (as.numeric(t1-sys1, units = "secs")*1000,
showModal(modalDialog(
title = "",
"Audio should play now"))
)
})
observeEvent(input$play, {
delay (as.numeric(t1-sys1, units = "secs")*1000,
output$my_audio1 <-renderUI({
tags$audio(src = "http://soundbible.com/grab.php?id=2213&type=mp3",
type = "audio/mp3", autoplay = TRUE)
})
)
})
observeEvent(input$play, {
delay ((as.numeric(t1-sys1, units = "secs")+20) *1000,
output$my_audio1 <-renderUI({
tags$audio(src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3",
type = "audio/mp3", autoplay = TRUE)
})
)
})
}
# Create Shiny app ----
shinyApp(ui, server)