Title: | 'Shiny' Extension of 'howler.js' |
---|---|
Description: | Audio interactivity within 'shiny' applications using 'howler.js'. Enables the status of the audio player to be sent from the UI to the server, and events such as playing and pausing the audio can be triggered from the server. |
Authors: | Ashley Baldry [aut, cre], James Simpson [aut] (Creator of howler.js) |
Maintainer: | Ashley Baldry <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0 |
Built: | 2025-03-01 04:06:26 UTC |
Source: | https://github.com/ashbaldry/howler |
howler
is used to initialise the 'howler.js' framework by adding all of the specified tracks to the
player, and can be run by either including UI buttons or server-side actions.
howler( tracks, options = list(), track_formats = NULL, auto_continue = FALSE, auto_loop = FALSE, seek_ping_rate = 1000L, elementId = NULL )
howler( tracks, options = list(), track_formats = NULL, auto_continue = FALSE, auto_loop = FALSE, seek_ping_rate = 1000L, elementId = NULL )
tracks |
A named vector of file paths to sounds. If multiple file extensions are included, then use a named list instead, with each list item containing each extension of the sound. |
options |
A named list of options to add to the player. For a full list of options see https://github.com/goldfire/howler.js?tab=readme-ov-file#options |
track_formats |
An optional list of formats of the sounds. By default 'howler' will guess the format to play in. Must be the same length as tracks |
auto_continue |
If there are multiple files, would you like to auto play the next file after the current
one has finished? Defaults to |
auto_loop |
Once all files have been played, would you like to restart playing the playlist?
Defaults to |
seek_ping_rate |
Number of milliseconds between each update of 'input${id}_seek' while playing. Default is set to 1000. If set to 0, then 'input${id}_seek' will not exist. |
elementId |
HTML id tag to be given to the howler player element |
All buttons associated with the howler
should be given the same id
argument. This is to ensure
that the buttons are linked to the player.
i.e. If howler(id = "howler")
, then howlerPlayButton(id = "howler")
A shiny.tag containing all of the required options for a Howl
JavaScript
object to be initialised in a shiny application.
On the server side there will be up to four additional objects available as inputs:
{id}_playing
A logical value as to whether or not the howler
is playing audio
{id}_track
Basename of the file currently loaded
{id}_seek
(If seek_ping_rate > 0
) the current time (in seconds) of the track loaded
{id}_duration
The duration (in seconds) of the track loaded
library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "howler", c(sound = "audio/sound.mp3")), howlerPlayPauseButton("howler") ) server <- function(input, output) { } shinyApp(ui, server) # Multiple file formats howler( elementId = "howler", list( track_1 = c("audio/sound.webm", "audio/sound.mp3"), track_2 = c("audio/sound2.webm", "audio/sound2.mp3"), ) )
library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "howler", c(sound = "audio/sound.mp3")), howlerPlayPauseButton("howler") ) server <- function(input, output) { } shinyApp(ui, server) # Multiple file formats howler( elementId = "howler", list( track_1 = c("audio/sound.webm", "audio/sound.mp3"), track_2 = c("audio/sound2.webm", "audio/sound2.mp3"), ) )
Output and render functions for using howler within Shiny applications and interactive Rmd documents.
howlerOutput(outputId) renderHowler(expr, env = parent.frame(), quoted = FALSE)
howlerOutput(outputId) renderHowler(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
expr |
An expression that generates a howler |
env |
The environment in which to evaluate |
quoted |
Is |
An output or render function that enables the use of the widget within Shiny applications.
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howlerOutput("howler"), howlerPlayPauseButton("howler") ) server <- function(input, output) { output$howler <- renderHowler(howler(c(sound = "audio/sound.mp3"))) } shinyApp(ui, server) }
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howlerOutput("howler"), howlerPlayPauseButton("howler") ) server <- function(input, output) { output$howler <- renderHowler(howler(c(sound = "audio/sound.mp3"))) } shinyApp(ui, server) }
Buttons that can be used to interact with the howler
.
howlerPlayButton
, howlerPauseButton
, howlerPlayPauseButton
and
howlerStopButton
will all be applied to the current track.
howlerBackButton
and howlerForwardButton
will
change the track position by a specified amount of time.
howlerPreviousButton
and howlerNextButton
will
play the previous/following track supplied to the player.
howlerVolumeDownButton
and howlerVolumeUpButton
will
change the volume of the player by a specified percentage.
howlerButton
is a customisable version of any of the above individual button.
howlerButton(howler_id, button_type = HOWLER_BUTTON_TYPES, ...) howlerPlayButton(howler_id) howlerPauseButton(howler_id) howlerPlayPauseButton(howler_id) howlerStopButton(howler_id) howlerBackButton(howler_id, seek_change = 10L) howlerForwardButton(howler_id, seek_change = 10L) howlerPreviousButton(howler_id) howlerNextButton(howler_id) howlerVolumeUpButton(howler_id, volume_change = 0.1) howlerVolumeDownButton(howler_id, volume_change = 0.1) howlerVolumeToggleButton(howler_id)
howlerButton(howler_id, button_type = HOWLER_BUTTON_TYPES, ...) howlerPlayButton(howler_id) howlerPauseButton(howler_id) howlerPlayPauseButton(howler_id) howlerStopButton(howler_id) howlerBackButton(howler_id, seek_change = 10L) howlerForwardButton(howler_id, seek_change = 10L) howlerPreviousButton(howler_id) howlerNextButton(howler_id) howlerVolumeUpButton(howler_id, volume_change = 0.1) howlerVolumeDownButton(howler_id, volume_change = 0.1) howlerVolumeToggleButton(howler_id)
howler_id |
ID given to the |
button_type |
Type of button to create. Available buttons are in the details, default set to |
... |
Attributes/Inner tags added to the button |
seek_change |
Time (in seconds) to move forward/backward the track when clicked. Default is 10 seconds |
volume_change |
How much to change the volume by. Default is 10%. |
The following button_type
are available to create:
play_pause
(default) Switch between playing and pausing the track
play
Resumes the current track
pause
Pauses the current track
stop
Stops current track, when played will start from beginning
previous
,next
Switches to the previous/following track
volumedown
,volumeup
Decreases/Increases the volume by 10%
(If using howlerButton
include the attribute `data-volume-change`
)
back
,forward
Seek forward/backwards 10s
(If using howlerButton
include the attribute `data-seek-change`
with negative values to go backwards)
When using a play_pause
button, the icon will toggle between the play and pause button
depending on whether or not the track is playing.
An HTML tag containing the audio button.
An additional input will be available in the server side in the form {id}_{button_type}
. For example
howlerBackButton("howler")
will create an input element of input$howler_back
. All of these will work in
the same way as actionButton
if (interactive()) { library(shiny) ui <- fluidPage( tile = "howler.js Player", howler(elementId = "howler", "audio/sound.mp3"), howlerPreviousButton("howler"), howlerBackButton("howler"), howlerPlayPauseButton("howler"), howlerForwardButton("howler"), howlerNextButton("howler"), howlerVolumeDownButton("howler"), howlerVolumeUpButton("howler") ) server <- function(input, output) { } shinyApp(ui, server) }
if (interactive()) { library(shiny) ui <- fluidPage( tile = "howler.js Player", howler(elementId = "howler", "audio/sound.mp3"), howlerPreviousButton("howler"), howlerBackButton("howler"), howlerPlayPauseButton("howler"), howlerForwardButton("howler"), howlerNextButton("howler"), howlerVolumeDownButton("howler"), howlerVolumeUpButton("howler") ) server <- function(input, output) { } shinyApp(ui, server) }
A way to display track information in the UI without having to communicate with the server.
howlerCurrentTrack(id, ...) howlerSeekTime(id, ...) howlerDurationTime(id, ...)
howlerCurrentTrack(id, ...) howlerSeekTime(id, ...) howlerDurationTime(id, ...)
id |
ID given to the current track label. For it to work with the |
... |
Other attributes to give to the HTML tag. |
A div
tag that will be linked to the howler
to show the current track.
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerCurrentTrack("sound"), p( howlerSeekTime("sound"), "/", howlerDurationTime("sound") ), howlerPlayPauseButton("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerCurrentTrack("sound"), p( howlerSeekTime("sound"), "/", howlerDurationTime("sound") ), howlerPlayPauseButton("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
A simple module containing a howler player and a default set of howler buttons. The module also contains the current position of the track being played and the duration of the track.
howlerModuleUI(id, files, ..., include_current_track = TRUE, width = "300px") howlerBasicModuleUI(id, files, ..., width = "300px") howlerModuleServer(id)
howlerModuleUI(id, files, ..., include_current_track = TRUE, width = "300px") howlerBasicModuleUI(id, files, ..., width = "300px") howlerModuleServer(id)
id |
ID to give to the namespace of the module. The howler player will have the ID |
files |
Files that will be used in the player. This can either be a single vector, or a list where different formats of the same file are kept in each element of the list. |
... |
Further arguments to send to |
include_current_track |
Logical, should the current track be included in the UI of the module? |
width |
Width (in pixels) of the player. Defaults to 400px. |
The UI will provide a player with a play/pause button, previous and next buttons, duration information and a volume slider.
The server-side module will return a list of reactive objects:
Logical value whether or not the player is currently playing
Name of the track currently loaded
Duration (in seconds) of the track currently loaded
Current position (in seconds) of the track currently loaded
if (interactive()) { ui <- fluidPage( title = "howler.js Module", howlerModuleUI("howl", c("audio/track1.mp3", "audio/track2.mp3")) ) server <- function(input, output, session) { howlerModuleServer("howl") } shinyApp(ui, server) }
if (interactive()) { ui <- fluidPage( title = "howler.js Module", howlerModuleUI("howl", c("audio/track1.mp3", "audio/track2.mp3")) ) server <- function(input, output, session) { howlerModuleServer("howl") } shinyApp(ui, server) }
A UI element that can be included with a howler
to manually change the location of the track.
howlerSeekSlider(id)
howlerSeekSlider(id)
id |
ID given to the volume slider. For it to work with the |
A slider element of class howler-seek-slider
that will display the position of the current track
playing.
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerPlayPauseButton("sound"), howlerSeekSlider("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerPlayPauseButton("sound"), howlerSeekSlider("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
Change the state of the howler player from the server.
playHowl
, pauseHowl
, togglePlayHowl
and stopHowl
will all be applied to the current track.
changeTrack
will update the track to the file specified.
addTrack
will add a new track to the specified player.
changeTrack(id, track, session = getDefaultReactiveDomain()) addTrack(id, track, play_track = FALSE, session = getDefaultReactiveDomain()) deleteTrack(id, track, session = getDefaultReactiveDomain()) playHowl(id, session = getDefaultReactiveDomain()) pauseHowl(id, session = getDefaultReactiveDomain()) togglePlayHowl(id, session = getDefaultReactiveDomain()) stopHowl(id, session = getDefaultReactiveDomain()) seekHowl(id, seek, session = getDefaultReactiveDomain()) changeHowlSpeed(id, rate = 1, session = getDefaultReactiveDomain())
changeTrack(id, track, session = getDefaultReactiveDomain()) addTrack(id, track, play_track = FALSE, session = getDefaultReactiveDomain()) deleteTrack(id, track, session = getDefaultReactiveDomain()) playHowl(id, session = getDefaultReactiveDomain()) pauseHowl(id, session = getDefaultReactiveDomain()) togglePlayHowl(id, session = getDefaultReactiveDomain()) stopHowl(id, session = getDefaultReactiveDomain()) seekHowl(id, seek, session = getDefaultReactiveDomain()) changeHowlSpeed(id, rate = 1, session = getDefaultReactiveDomain())
id |
ID of the |
track |
Either the track name of the file to change to, or the index of the file to play. If the file is not included in the player nothing will happen. |
session |
Shiny session |
play_track |
Logical, should the new track be played on addition? |
seek |
Time (in seconds) to set the position of the track |
rate |
Rate (from 0.5 to 4.0) of the audio playback speed |
For 'deleteTrack', make sure that the name is used of the track rather than the file name.
Updates the the state of the specified howler
in the shiny application.
if (interactive()) { library(shiny) tracks <- c("audio/track1.mp3", "audio/track2.mp3") ui <- fluidPage( title = "howler.js Player", selectInput("track", "Select Track", basename(tracks)), howler(elementId = "howler", tracks), howlerPlayPauseButton("howler") ) server <- function(input, output) { observeEvent(input$track, changeHowlerTrack("howler", input$track)) } runShiny(ui, server) }
if (interactive()) { library(shiny) tracks <- c("audio/track1.mp3", "audio/track2.mp3") ui <- fluidPage( title = "howler.js Player", selectInput("track", "Select Track", basename(tracks)), howler(elementId = "howler", tracks), howlerPlayPauseButton("howler") ) server <- function(input, output) { observeEvent(input$track, changeHowlerTrack("howler", input$track)) } runShiny(ui, server) }
A more user friendly way to adjust the volume of the howler
than by using buttons. There are
still volume up/down buttons, but a slider can be moved up/down as required.
howlerVolumeSlider(id, volume = 1, button = TRUE)
howlerVolumeSlider(id, volume = 1, button = TRUE)
id |
ID given to the volume slider. For it to work with the |
volume |
Initial volume to set the player at. Defaults at 100% |
button |
Logical, should a mute toggle button be included next to the slider? Default is |
If using howlerVolumeSlider
, avoid using the volume buttons, as this will cause duplicate IDs to appear in the
shiny application and prevents the slider from working properly.
A volume slider with a howlerVolumeDownButton
and a howlerVolumeUpButton
either side.
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerPlayPauseButton("sound"), howlerVolumeSlider("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
if (interactive()) { library(shiny) ui <- fluidPage( title = "howler.js Player", howler(elementId = "sound", "audio/sound.mp3"), howlerPlayPauseButton("sound"), howlerVolumeSlider("sound") ) server <- function(input, output, session) { } shinyApp(ui, server) }
Using howler.js, play a sound within a shiny application.
playSound(track, options = NULL, session = getDefaultReactiveDomain())
playSound(track, options = NULL, session = getDefaultReactiveDomain())
track |
Either a URL, file path or Base 64 character string of the sound to play |
options |
A named list of options to add to the sound. For a full list of options see https://github.com/goldfire/howler.js?tab=readme-ov-file#options |
session |
Shiny session |
The 'src' and 'autoplay' options are pre-determined by 'playSound' and are not required in 'options'. If they are included, a warning will be printed and they will be ignored from the list.
library(shiny) audio_files_dir <- system.file("examples/_audio", package = "howler") addResourcePath("sample_audio", audio_files_dir) ui <- fluidPage( actionButton("play", "Play Sound") ) server <- function(input, output) { observeEvent(input$play, { playSound("sample_audio/running_out.mp3") }) } shinyApp(ui, server)
library(shiny) audio_files_dir <- system.file("examples/_audio", package = "howler") addResourcePath("sample_audio", audio_files_dir) ui <- fluidPage( actionButton("play", "Play Sound") ) server <- function(input, output) { observeEvent(input$play, { playSound("sample_audio/running_out.mp3") }) } shinyApp(ui, server)
{howler}
Example ApplicationsRun {howler}
Example Applications
runHowlerExample(example = "basic", display.mode = "showcase", ...) availableHowlerExamples()
runHowlerExample(example = "basic", display.mode = "showcase", ...) availableHowlerExamples()
example |
Name of the example to load. Current examples include:
|
display.mode |
The mode in which to display the application. By default set to |
... |
Optional arguments to send to |
This function does not return a value; interrupt R to stop the application (usually by pressing Ctrl+C or Esc).
availableHowlerExamples() if (interactive()) { library(shiny) library(howler) runHowlerExample("basic") }
availableHowlerExamples() if (interactive()) { library(shiny) library(howler) runHowlerExample("basic") }