5

I would like a Shiny app to play a sound after a reactive event.

I already know a solution to this at the end of an R script.

On Shiny I tried:

library(shiny)
library(beepr)

ui <- fluidPage(
    tags$head(tags$script(src = "message-handler.js")),
    actionButton("dobeep", "Play sound")
)

server <- function(input, output, session) {
    observeEvent(input$dobeep, {
        #Beeps on local machine/server
        beepr::beep()

        #Doesn't beep on client
        insertUI(selector = "#dobeep",
                 where = "afterEnd",
                 ui = tags$audio(src = "beep.wav", type = "audio/wav", autoplay = T, controls = NA, style="display:none;")
        )
    })
}

shinyApp(ui, server)

I put beep.wavin the app.R directory.
On local machine, I hear the beepr::beep(), but I don't hear the audio tag from the client.
In client/server mode, I hear nothing.
In both cases, the audio Tag doesn't seem to work.
Thanks for your help.

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • "In client/server mode": do you mean on a *remote* server? if so, did you upload the wav file as well as the source code for the app? – Limey Jun 14 '20 at 07:34
  • yes, I uploaded both files on the remote server. – Waldi Jun 14 '20 at 07:37
  • you could try to base64encode the file and give that as input? See https://stackoverflow.com/questions/56926161/uploaded-audio-files-does-not-play-in-r-shiny . That works for me with your example. – Vandenman Jun 14 '20 at 07:40
  • OK. There's obviously a difference between your local set up and the remote set up right? Could it be permissions? Does a `file.exists` tell you that the file can be found on the remote server> etc, etc... – Limey Jun 14 '20 at 07:41
  • I put the beepr test in local mode to make sure the event is triggered, but actually I don't hear the beep.wav in local mode or remote mode : see my edit – Waldi Jun 14 '20 at 07:47
  • Your questions led me to the [solution](https://stackoverflow.com/questions/36205419/r-shiny-audio-playback). Thanks for your help! – Waldi Jun 14 '20 at 07:53

1 Answers1

3

The beep.wav file should be in the /www folder, located in the same directory as the shiny app for the audio Tag to work, see following post.

This works :

library(shiny)

ui <- fluidPage(
    tags$head(tags$script(src = "message-handler.js")),
    actionButton("dobeep", "Play sound")
)

server <- function(input, output, session) {
    observeEvent(input$dobeep, {
        insertUI(selector = "#dobeep",
                 where = "afterEnd",
                 # beep.wav should be in /www of the shiny app
                 ui = tags$audio(src = "beep.wav", type = "audio/wav", autoplay = T, controls = NA, style="display:none;")
        )
    })
}

shinyApp(ui, server)
Waldi
  • 39,242
  • 6
  • 30
  • 78