autoPlay | R Documentation |
Play Audio without an Explicit Play Button
autoPlay(audioId, src)
audioId |
character. The id of the audio element. |
src |
character. The name of a WAV file in the www folder. |
When included in a reactive observer (observe()
or observeEvent()
), plays an audio file without a click from a participant.
# NOTE: This example will only play audio from a saved app script
# in the same directory as a 'www' subdirectory that contains the audio file.
data("samp_wav")
# create www if it does not exist
if (!dir.exists("www")) {dir.create("www")}
tuneR::writeWave(samp_wav, "www/sample.wav")
library(shiny)
ui <- tabsetPanel(id = "tabs",
tabPanel(
"tab1",
"The sound is not playing yet. Click on tab2 to play the sound."
),
tabPanel(
"tab2",
"The sound should be playing now."
)
)
server <- function(input, output, session) {
observeEvent(input$tabs, {
if (input$tabs == "tab2") {
autoPlay(audioId = "sample", src = "sample.wav")
}
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.