我想在Shiny
播放音频文件。更具体地说,我希望能够回放和转发音频文件。
每当我使用互联网上的文件时,这都可以。但是,如果我将相同的文件放在www
文件夹中,音频仍然可以播放,但倒带和转发不再有效。
我希望这与Accept-Ranges
HTTP标头有关,同样如下所述
Setting HTML5 audio position
事实上,从https网址播放时,accept-ranges
标头设置为bytes
:
下面给出了可重现的代码。我的问题;这是Shiny中的一个问题,还是我忽略了什么?有没有办法为accept-ranges
目录中的文件设置HTTP标头www
?
文件夹结构
.
├── app.R
├── www
│ ├── Jazz_Trombone.ogg
app.R
library(shiny)
ui <- fluidPage(
# Forwarding and rewinding works fine.
#tags$audio(id='my_audio_player',src = 'https://upload.wikimedia.org/wikipedia/commons/8/87/Jazz_Trombone.ogg',type="audio/mpeg", autostart = '0', controls = TRUE)
# Forwarding and rewinding does not work.
tags$audio(id='my_audio_player',src = 'Jazz_Trombone.ogg',type="audio/mpeg", autostart = '0', controls = TRUE)
)
server <- function(input, output, session){
}
答案 0 :(得分:2)
不确定您是否正在运行闪亮服务器...所以我将提供一个不包括编辑匿名配置文件的解决方案。这适用于本地(win7x64)和linux(ubuntu)......
我真正做的只是base64
对文件进行编码,并将其传递给html标记。
app.audio <- function(url = NULL, ...){
https_url <-
'https://upload.wikimedia.org/wikipedia/commons/8/87/Jazz_Trombone.ogg'
local_path <- "inst/www/Jazz_Trombone.ogg"
as_b64 <- markdown:::.b64EncodeFile(local_path)
ui <- fluidPage(
column(4, tags$h4('https via url'),
tags$audio(
controls = "controls",
tags$source(
src = https_url,
type='audio/ogg; codecs=vorbis')
)),
column(4, tags$h4('via local_path'),
tags$audio(
controls = "controls",
tags$source(
src = local_path,
type='audio/ogg; codecs=vorbis')
)),
column(4, tags$h4("via base 64 encoding"),
tags$audio(
controls = "controls",
tags$source(
src = as_b64,
type='audio/ogg; codecs=vorbis')
))
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
}
结果是:
> app.audio()
因此,本地路径不起作用,但请查看?shiny::addResourcePath
以在应用中设置有效的全局路径;这可能也有用。
能够让每次尝试都使用控件的全部功能...在Windows上本地运行以模仿您的设置,添加资源路径似乎可以解决所有问题。
app.audio <- function(url = NULL, ...){
# Instruct shiny to add this resource path so we can call/source files from
# the /inst/www with only www
addResourcePath(prefix = "www", "./inst/www")
https_url <-
'https://upload.wikimedia.org/wikipedia/commons/8/87/Jazz_Trombone.ogg'
local_path <- "inst/www/Jazz_Trombone.ogg"
as_b64 <- markdown:::.b64EncodeFile(local_path)
ui <- fluidPage(
column(4, tags$h4('https via url'),
tags$audio(
controls = "controls",
tags$source(
src = https_url,
type='audio/ogg; codecs=vorbis')
)),
column(4, style="background:#F5F5F5", tags$h4('via local_path'),
fluidRow(tags$h5("using htmltools::tag"),
tags$audio(
controls = "controls",
tags$source(
src = "www/Jazz_Trombone.ogg", # changing to resource-path prefix
type='audio/ogg; codecs=vorbis')
)),
fluidRow(tags$h5("Using good-ole html"),
# Testing with straight up html
HTML(paste0(
c('<audio controls>',
' <source src="www/Jazz_Trombone.ogg" type="audio/ogg; codecs=vorbis">',
'</audio>'),
collapse = "\n")))),
column(4, tags$h4("via base 64 encoding"),
tags$audio(
controls = "controls",
tags$source(
src = as_b64,
type='audio/ogg; codecs=vorbis')
))
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
}