gpt4 book ai didi

r - 如果未知路径/来源,如何在Shiny中播放音频文件?

转载 作者:行者123 更新时间:2023-12-02 22:25:29 27 4
gpt4 key购买 nike

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

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

但是,如果在 session 期间生成了音频文件,则找不到使用tag $ 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

我现在的问题是,如何在没有 tags$audio(src = "", type = "audio/wav")路径的情况下使用 src = ""进行“测试”?

最佳答案

一种可能是将生成的文件复制到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)

关于r - 如果未知路径/来源,如何在Shiny中播放音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com