autoPlay: Play Audio without an Explicit Play Button

View source: R/autoPlay.R

autoPlayR Documentation

Play Audio without an Explicit Play Button

Description

Play Audio without an Explicit Play Button

Usage

autoPlay(audioId, src)

Arguments

audioId

character. The id of the audio element.

src

character. The name of a WAV file in the www folder.

Value

When included in a reactive observer (observe() or observeEvent()), plays an audio file without a click from a participant.

Examples

# 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")
    }
  })
}


abbey-thomas/speechcollectr documentation built on Nov. 19, 2024, 7:09 p.m.