recordServer | R Documentation |
The server function for recording user audio enables the 'stop' button after the user begins recording and checks to make sure the user has given the website permission to record audio in their browser. Requires the UI recordUI
.
recordServer(
id = "record",
outFile,
attempts = Inf,
overwrite = FALSE,
writtenStim = NULL,
writtenDelay = 500
)
id |
The input ID associated with the record module. Must be the same as the id of |
outFile |
Character or reactive expression. Where to store the audio file. Can indicate any subdirectory of the present working directory. If dynamic, wrap in |
attempts |
Numeric (Defaults to Inf). How many attempts to create this recording should the participant be allowed? |
overwrite |
Boolean. Defaults to |
writtenStim |
Either a character vector (for a single, static stimulus) or a reactive expression (created with reactive, for a stimulus that should be updated from trial to trial) representing a written stimulus that a participant will read while recording. |
writtenDelay |
Integer. How many milliseconds should elapse between the time the participant clicks |
Returns a reactive expression containing: (1) n
: the number of attempts at recording the current file and (2) file
: the filename where only the most recent attempt is saved (previous attempts have been overwritten). Also returns a wav file in a filename comprising outPrefix
and a unique four digit number.
Must be placed at the top level of the application's server function. The "start" and "stop" buttons from this module can be accessed in the server code with the "id" of the module as follows: input[["id-start"]]
or input[["id-stop"]]
. The submit button can be accessed with input[["id-submit"]]
.
Must be used with recordUI
. For a more flexible audio recording protocol, see startRec
, stopRec
, and useRecorder
.
Other Audio recording module:
recordUI()
if (interactive()) {
library(shiny)
library(shinyjs)
# Build UI
ui <- fluidPage(
## Get background javascript ready
useShinyjs(),
## Setup page layout
div(id = "trialDiv",
style = "text-align:center;",
actionButton("next_trial", "NEXT"),
### Initialize the recording interface invisibly
hidden(recordUI(id = "rec_module"))
)
)
# The Server function
server <- function(input, output, session) {
## Create an object that will count trials
rvs <- reactiveValues(trial_n = 0)
## When the participant clicks "NEXT"...
observeEvent(input$next_trial, {
### Increase the trial number
rvs$trial_n <- rvs$trial_n + 1
### Show the recording interface
showElement("rec_module")
### Hide the next button
hide("next_trial")
})
## Note that the call to recordServer() is at the top level of our app's server function
## And the returned filename and text to be read are wrapped in reactive()
## (since they need to be reactive).
recordServer(id = "rec_module",
attempts = 3, overwrite = TRUE,
outFile = reactive(paste0("rec", rvs$trial_n, ".wav")),
writtenStim = reactive(paste0("This is recording ", rvs$trial_n, ".")))
## As with all speechcollectr modules, the submit button's Id
## can be accessed with the module id + `-submit`
## Here when the user clicks submit inside the recording module...
observeEvent(input[["rec_module-submit"]], {
## Make the next button reappear
showElement("next_trial")
})
}
# Run the application
shinyApp(ui = ui, server = server)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.