rshinyhowler.js

Music files do not play in shiny


Goal

Users play music on a shiny app

Code

I am trying to use the Shiny howlerjs extension to let users play music on a shiny app. Following is an example from the package repo that plays fine:

library(shiny)
library(howler)

audio_files_dir <- system.file( "examples/_audio", package = "howler")
addResourcePath("sample_audio", audio_files_dir)
audio_files <- file.path("sample_audio", list.files(audio_files_dir, ".mp3$"))


ui <- fluidPage(
  title = "howler Example",
  useHowlerJS(),
  
  h3("Howler Example"),
  howlerPlayer("sound", audio_files),
  howlerSeekSlider("sound"),
  howlerPreviousButton("sound"),
  howlerBackButton("sound"),
  howlerPlayPauseButton("sound"),
  howlerForwardButton("sound"),
  howlerNextButton("sound"),
  howlerVolumeSlider("sound"),
  tags$br(),
  tags$br(),
  tags$p(
    "Track Name:",
    textOutput("sound_track", container = tags$strong, inline = TRUE)
  ),
  tags$p(
    "Currently playing:",
    textOutput("sound_playing", container = tags$strong, inline = TRUE)
  ),
  tags$p(
    "Duration:",
    textOutput("sound_seek", container = tags$strong, inline = TRUE),
    "/",
    textOutput("sound_duration", container = tags$strong, inline = TRUE)
  )
)

server <- function(input, output, session) {
  output$sound_playing <- renderText({
    if (isTRUE(input$sound_playing)) "Yes" else "No"
  })
  
  output$sound_duration <- renderText({
    sprintf(
      "%02d:%02.0f",
      input$sound_duration %/% 60,
      input$sound_duration %% 60
    )
  })
  
  output$sound_seek <- renderText({
    sprintf(
      "%02d:%02.0f",
      input$sound_seek %/% 60,
      input$sound_seek %% 60
    )
  })
  
  output$sound_track <- renderText({
    req(input$sound_track)
    sub("\\.\\w+$", "", basename(input$sound_track))
  })
}

shinyApp(ui, server)

The audio_files contain the sample files that the package author provided:

> file.path("sample_audio", list.files(audio_files_dir, ".mp3$"))
[1] "sample_audio/80s_vibe.mp3"    "sample_audio/rave_digger.mp3"
[3] "sample_audio/running_out.mp3"  

Problem

I make just 1 change. I replace the audio_files code with a path to my audio files as follows:

audio_files <- list.files(path = "Music/", pattern= ".mp3$", full.names = TRUE)

> list.files(path = "Music/", pattern= ".mp3$", full.names = TRUE)
[1] "Music/l1.mp3" "Music/l2.mp3" "Music/l3.mp3"

Everything else remains the same. The app opens without any error/warning. But the new music files do not play. Two questions:

  1. What am I doing wrong in the above code that my local audio files do not play in the app but do play externally?
  2. Please recommend other user-interfaces for selecting music within shiny.

Solution

  • In order to use music from the new directory, you will also need to change addResourcePath to the relevant path. In this case, if you use addResourcePath("Music", "Music") this should be enough for it to work.