如果没有路径/来源,如何在Shiny中播放音频文件?

时间:2018-04-30 08:56:45

标签: r audio shiny

对于Shiny应用程序,我希望能够播放会话期间生成的音频文件。 如果是我要上传的音频文件,我会使用

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

但是,如果在会话期间生成音频文件,我找不到使用标签$ audio的方法,因此我没有文件名或路径。 有关如何播放此类音频文件的任何建议?谢谢!

编辑:我添加了一个简短的可重复的例子。希望我能做的更清楚。

    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

我现在的问题是如何玩&#34;测试&#34;使用tags$audio(src = "", type = "audio/wav"),没有src = ""的路径?

1 个答案:

答案 0 :(得分:1)

一种可能性是将生成的文件复制到www文件夹,然后使用renderUI创建音频标记。下面是一个如何实现这一目标的示例。希望这有帮助!

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)