上载的音频文件无法正常播放

时间:2019-07-07 21:13:30

标签: r shiny shinydashboard

我开发了一个应用程序,该应用程序将.wav文件作为输入并播放。但是,它似乎不起作用。另一方面,如果将音频文件放在/ www文件夹中,并且给出了路径名,则可以正常播放。

我在做什么错了?

app.R

library( shinydashboard )

ui = source( file.path( "ui", "ui.R" ), local = T )$value   #..... ui for ocr

server = function( input, output, session ){

  #..... Include server logic for each tab .....

  source( file.path( "server", "server.R" ), local = T )$value    #..... server logic for ocrs

}

shinyApp( ui = ui, server = server )

ui.R

header = dashboardHeader( title = 'Speech 2 Text' )

sidebar =  dashboardSidebar( collapsed = F,

  sidebarMenu(

  menuItem( strong( '  Speech to Text' ), tabName = 'tab1' )

  )

)

body =  dashboardBody(

  tabItems(

    #####.... tab1 ....####

    tabItem( tabName = 'tab1',

             fluidRow(

               box( title = 'Actions', status = 'success', collapsible = T, width = 12, solidHeader = T,

                    column( 6, fileInput( 'uploaded_audio', 'Choose WAV File', multiple = FALSE, accept = '.wav' ) ),

                    br(),

                    column( 3, align = 'left',

                            actionButton( 'play_audio', 'Play Audio' ),

                            tags$style( "#play_audio { vertical-align: middle; height: 30px; width: 70%; font-size: 15px;color: white;background-color:#1B618D;border-color: #374645 }" )

                    )

                  )

             )

    )

  )

)

dashboardPage( header, sidebar, body, skin = 'yellow' )

server.R

observeEvent( input$play_audio, {

  req( input$uploaded_audio )

  insertUI( selector = "#play_audio", where = "afterEnd",

           ui = tags$audio( src = input$uploaded_audio$datapath, type = "audio/wav", autoplay = NA, controls = NA )  
  )

})

我还打印了str( input$uploaded_audio )来查看文件的存储路径。

它显示如下输出:

data.frame':    1 obs. of  4 variables:
 $ name    : chr "newOSR_us_000_0034_8k.wav"
 $ size    : int 568810
 $ type    : chr "audio/wav"
 $ datapath: chr "C:\\Users\\MACHIN~1\\AppData\\Local\\Temp\\Rtmp2zx4tW/c3d8af2a9ed3e4b8cd415aea/0.wav"

1 个答案:

答案 0 :(得分:0)

您可以对文件进行base64编码:

library(base64enc)

server <- function(input, output){
  observeEvent( input$play_audio, {

    req( input$uploaded_audio )

    base64 <- dataURI(file = input$uploaded_audio$datapath, mime = "audio/wav")

    insertUI( selector = "#play_audio", where = "afterEnd",

              ui = tags$audio( src = base64, type = "audio/wav", autoplay = NA, controls = NA )  
    )

  })
}