2

For a Shiny app, I want to be able to play audio files that are generated during the session itself. If it were an audio-file that I want to upload, I would use

    tags$audio(src = "www/name.wav", type = "audio/wav")

However, I could not find a way to use tags$audio if the audio file is generated during the session, and thus I do not have a filename or path. Any suggestions on how I can play such audio files? Thanks!

EDIT: I added a short reproducible example. Hopefully it becomes more clear what I am trying to do.

    url <- "http://www.wavlist.com/humor/001/911d.wav"

    # Define the temporary directory and download the data
    dest_path <- "sound.wav"
    download.file(url,destfile = dest_path)

    # Load the audio file
    test <- audio::load.wave(dest_path)

    # Change something small to this audio file
    test <- test + 0.3

My question now is how can I play "test" using tags$audio(src = "", type = "audio/wav"), without having a path for the src = "" ?

AES
  • 175
  • 8

1 Answers1

2

One possibility would be to copy the generated file to the www folder, and use renderUI to create your audio tag. Below is an example on how you could achieve that. Hope this helps!

library(shiny)
library(shinyjs)
library(audio)
library(seewave)

ui <- fluidPage(
  textInput('my_url','URL:',value="http://www.wavlist.com/humor/001/911d.wav"),
  uiOutput('my_audio')
)

server <- function(input, output, session){

  # Render the audio player
  output$my_audio <- renderUI({

    url <- input$my_url

    # Define the temporary directory and download the data
    dest_path <- "sound.wav"
    download.file(url,destfile = dest_path)
    # Load the audio file
    test <- audio::load.wave(dest_path)
    # Change something small to this audio file
    test <- test + 0.3
    savewav(test,filename = 'www/myaudio.wav')

      tags$audio(id='my_audio_player',
                 controls = "controls",
                 tags$source(
                   src = markdown:::.b64EncodeFile('www/myaudio.wav'),
                   type='audio/ogg; codecs=vorbis'))

  })
}

shinyApp(ui = ui, server = server)
Florian
  • 24,425
  • 4
  • 49
  • 80
  • Thanks for your answer Florian! It does push me in the right direction (I think), but the problem I am now facing is the following: I am generating an audio-variable (which is basically a list of doubles), so I do not have the datapath of it, and thus I cannot use file.copy(from, to). Any suggestions on how I could "download" this audio-variable to the www folder? – AES Apr 30 '18 at 14:24
  • 1
    @AES, if you are looking for a more specific answer, please consider creating a [reproducible example](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable/48343110). I am not really sure what you are asking. – Florian Apr 30 '18 at 14:56
  • I added a reproducible example. My apologies! :) – AES Apr 30 '18 at 15:46
  • 1
    @AES, I modified my example to closer match your desired behavior. Let me know if this helps! – Florian Apr 30 '18 at 16:38
  • 1
    Yep it helped! Thanks a lot! – AES May 01 '18 at 07:35