1

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)







 
Bahi8482
  • 489
  • 5
  • 15
  • Your code works fine for me. It plays the audio after 5 seconds, as set-up. – YBS Apr 29 '21 at 01:31
  • @YBS It is still not working, any explanation you can think ok? I updated the shiny package to the latest, but still not working. thank you – Bahi8482 Apr 29 '21 at 04:05
  • You can restart your RStudio and/or laptop/PC. I am running on Windows and it works fine. – YBS Apr 29 '21 at 10:57
  • I will try to restart too. I also found this answer which mentioned that audio might not work in the rstudio pane but should work in chrome/other browsers https://stackoverflow.com/a/39380988/12875646 – Bahi8482 Apr 29 '21 at 12:02
  • Yes, I always Run External. – YBS Apr 29 '21 at 12:13
  • @YBS I deployed the shiny app and when I try to run it from phone (iphone, safari), sound does not work. apparently it has to do with autoplay policy for mobile browsers. any thoughts on how to solve that ? – Bahi8482 Apr 30 '21 at 21:50
  • To trick the autoplay restriction, try my answer [here](https://stackoverflow.com/questions/67206707/r-shiny-not-playing-audio-automatically/67292818#67292818) – YBS Apr 30 '21 at 22:15
  • @YBS this looks good indeed. I could not try it in my case yet as I do not know how to add two audio files inside the same `renderUI` function. any idea how to write this in shiny? thank you – Bahi8482 May 01 '21 at 19:51
  • You should call the `silence.mp3` audio file in the `ui` as shown in my answer, and not in the `renderUI`. – YBS May 01 '21 at 20:50
  • @YBS I tried and updated the code in the question above. It is still working from laptop but not from phone. would you mind having a look and let me know if you have any suggestions? – Bahi8482 May 02 '21 at 17:24
  • Sorry, this might be a Safari issue. I have only tested on Chrome/Edge, and it works fine. – YBS May 02 '21 at 18:57

0 Answers0